Examples of VB Access continued:
In this example, I am using a nested IF. What I am testing and what I do is really useless, just focus on the structure of the IF.
The New Amount is a result of these calculations. Note also that the Amount % must be entered by the user. I set this up as a percent field as seen below.
Private Sub cmdCalcNewAmt_Click() Dim wkAns As Single Dim wkAmtCont As Single, wkMsg As String If AmtCont <> "" Then wkAmtCont = Val(AmtCont) If DriveNo = "123" Then If wkAmtCont > 500 Then wkAns = Val(wkAmtCont) + Val(wkAmtCont) * Val(txtAmtPer) + 100 Else wkAns = Val(wkAmtCont) + Val(wkAmtCont) * Val(txtAmtPer) End If Else wkAns = Val(wkAmtCont) + Val(wkAmtCont) * Val(txtAmtPer) - 50 End If txtNewAmt = wkAns Else wkMsg = "AmtCont is null " & 100 & " will be entered as the New Amount" MsgBox (wkMsg) txtNewAmt = 100 End If End Sub
In the code above, I added wkMsg as String (character/text data) and generated a message that concatenated words with a number. I could also have concatenated with the contents of a field as shown below. Please note that in VB this was entered as 1 line - your browser may show it differently.
wkMsg = "AmtCont is null " & 100 & " entered as New Amount for " & txtIdno
This code now concatenates words with an amount and then with words and finally with the contents of the id # which is in txtIdno (note that I changed the name to be more Visual Basic conforming).
It should be noted that the code above worked and I tried it because I was experimenting. The best way to do concatenation is to concatenate like fields which means that when I first did this I put str(100) which converts numbers to string fields. The txtIdno was already a string, so that was not a problem. This code is shown below (please note that it is entered as 1 line in VB your browser may show it differently):
wkMsg = "AmtCont is null " & Str(100) & " entered as New Amount for " & txtIdno
If you want to output to appear on two lines instead of one, you can modify the wkMsg to include a carriage control, line feed. Again, please note this was 1 line in VB - your browser may show it differently.
wkMsg = "AmtCont is null " & Str(100) & " entered as New Amount" & vbCrLf & "For " & txtIdno