Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with messageboxes 1

Status
Not open for further replies.

welshone

Programmer
Jul 30, 2001
414
GB
Hello,
I am attaching this code to a button on click :

If cmbitem Is Null Then

MsgBox ("Please select an item")

End If

But when I click the button I get object required.

what am I doing wrong ?

thank you
Jamie
 
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!"
 
cheers, but its still not working !

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. ?

If cmbitem = "" Then

MsgBox "Please select an item"

End If
 
hello,
this is all my code, can somebody help me out.
I'm sure it is very simple :

Private Sub cmdsaveadd_Click()
On Error GoTo Err_cmdsaveadd_Click

'cmbitem = ""
'IsNull(Len(cmbitem)) = True

If cmbitem = "" Then

MsgBox "Please select an item"


If cmbgeneraldesc = "" Then

MsgBox "Please select a general description"


If cmbstatus = "" Then

MsgBox "Please select a status"

If cmbage = "" Then

MsgBox "Please select an Age Range"

If cmbdestschool = "" Then

MsgBox "Please select a destination school"

Else

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

DoCmd.GoToRecord , , acNewRec

End If
End If
End If
End If
End If

Exit_cmdsaveadd_Click:
Exit Sub

Err_cmdsaveadd_Click:
MsgBox Err.Description
Resume Exit_cmdsaveadd_Click

End Sub


cheers.
Jamie
 
This is not related to your msgbox.

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!"
 

they are foreign keys being inserted into the table, but instead of eror trapping can't I do it though if, then statements ?

IF I do this :


If IsNull(Len(cmbitem)) = True Then

MsgBox "Please select an item"


If IsNull(Len(cmbgeneraldesc)) = True Then

MsgBox "Please select a general description"


If IsNull(Len(cmbstatus)) = True Then

MsgBox "Please select a status"


If IsNull(Len(cmbage)) = True Then

MsgBox "Please select an Age Range"


If IsNull(Len(cmbdestschool)) = True Then

MsgBox "Please select a destination school"

Else

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

DoCmd.GoToRecord , , acNewRec

End If
End If
End If
End If
End If


it displays the messages if all are blank, but if I fill in one of two of the combos then nothing happens ?

 
I responded before I saw all your code.

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 &quot;The Key, The Whole Key, and Nothing But The Key, So Help Me Codd!&quot;
 
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 &quot;Please select an item&quot;


If cmbgeneraldesc < 1 Then

MsgBox &quot;Please select a general description&quot;


If cmbstatus < 1 Then

MsgBox &quot;Please select a status&quot;


If cmbage < 1 Then

MsgBox &quot;Please select an Age Range&quot;


If cmbdestschool < 1 Then

MsgBox &quot;Please select a destination school&quot;

Else

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

DoCmd.GoToRecord , , acNewRec

End If
End If
End If
End If
End If
 
Rather than wait until the user has skipped by all five (or seven or eighty-leven) comboix, why not check in the Lost_Focus of each combo:

Sub CmbItem1_LostFocus
if IsNull(Me!cmbItem1) then
msgbox &quot;Sorry, you must pick an option&quot;
Cancel = True
End if
end sub

This cancels the LostFocus event if no choice has been made in the combo.

Jim
 
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 &quot;A&quot;
Else
If B Then
Msgbox &quot;B&quot;
Else
If C Then
Msgbox &quot;C&quot;
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.

&quot;The Key, The Whole Key, and Nothing But The Key, So Help Me Codd!&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top