When you call a function with parentheses it wants to return a value so you would call it like this:
a = Msgbox ("Hey"
You might use the above example if you need to do something conditionally based on the reply, something like this:
Dim a As Long
a = Msgbox ("Do you like Icecream?", vbYesNo)
Select Case a
Case vbYes
'place the order
Case vbNo
'don't
Case Else
'?
End Select
Since you are not using a return value call it without the parentheses like this:
Msgbox "Hey"
"The Key, The Whole Key, and Nothing But The Key, So Help Me Codd!"
I have a list box, and If nothing is selected when I click the add button I want it to bring up my alert, at present it brings up the standard access mssage : you can't add a record becuase a related record is requiredin table tblitem. ?
I take it that the list box selects a record for a foreign key defined in a table relationship. You could probably trap the actual error and then display your message instead of the error message.
On Error GoTo HandleErr
'put your add code here
ExitHere:
Exit Sub
HandleErr:
Select Case Err.Number
Case 0
'put errors to ignore here
Resume Next
'Case 'Put the error number that you need to trap here
MsgBox "Please select an item"
Case Else
MsgBox "Error " & Err.Number & ": " _
& Err.Description, vbCritical
End Select
Resume ExitHere:
Resume 0 'for debugging
"The Key, The Whole Key, and Nothing But The Key, So Help Me Codd!"
When you examine the value of a combo in code you are 'seeing' the bound column which isn't necessarily what is displayed. Is the bound column in your item combo a long integer? If you are looking up against another table it probably is. In that case you should be testing cmbitem for an integer value instead of null or "".
If cmbitem < 1 then "The Key, The Whole Key, and Nothing But The Key, So Help Me Codd!"
of course !
I have now added this code in place of the other stuff, but It is still not working correctly.
Basically if one of the combo boxes isn't filled in then display an error relating to the appropriate box, other wise if all combo boxes are filled save record and add a new record :
all it does is show the first alat, but then when I click on the button again it does nothing ? :
If cmbitem < 1 Then
MsgBox "Please select an item"
If cmbgeneraldesc < 1 Then
MsgBox "Please select a general description"
If cmbstatus < 1 Then
MsgBox "Please select a status"
If cmbage < 1 Then
MsgBox "Please select an Age Range"
If cmbdestschool < 1 Then
MsgBox "Please select a destination school"
Your If statements won't work that way for what you want to do. If the first If statement evaluates to True it goes to End If, skipping all the rest. Try the following:
If A Then
Msgbox "A"
Else
If B Then
Msgbox "B"
Else
If C Then
Msgbox "C"
Else
...
End If
End If
End If
When debugging this sort of thing it is very useful to set a breakpoint in code and watch as each line is executed so that you can see what is happening. As an exercise, and to see what I mean, you can try the following (before you make any code changes):
Put the curser in the first line of your sub, click F9 and run the code as normal. You will see that the code halts on the first line and highlites it in yellow. Press F8 to execute that line and move to the next. Keep pressing F8 to continue line by line. You will quickly see which lines are executing and which are not.
Then make the code changes and do this again and you should see all your If statements being evaluated.
WildHare's idea would make things a bit cleaner, however it would only work if every control gets focus at some point during data entry. In the world of Windows we (programmers) can't be sure of that. There is nothing to prevent a user from skipping right by certain controls by using the mouse.
"The Key, The Whole Key, and Nothing But The Key, So Help Me Codd!"
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.