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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Suppressing the opening of a form

Status
Not open for further replies.

papic1972

Technical User
Apr 10, 2003
209
AU
Hi all,

I have a form "frmCardFile" that has a field "cboCustomer" & a command button "cmdViewJobs" that opens up another form "frmJob".

My intention is to stop the user if they click on "cmdViewJobs" from opening "frmJob" if nothing has been selected from "cboCustomer" & to pop-up a msgbox that states something like "you haven't selected anything". If the user then goes back & selects a customer, then clicks cmdOpenJob, the user should then be allowed to open "frmJob". I have the following code in the "onClick" event of "cmdViewJobs":

Private Sub cmdViewJobs_Click()
On Error GoTo Err_cmdViewJobs_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmJob"
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list" _
, vbExclamation
Exit Sub
End If
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_cmdViewJobs_Click:
Exit Sub

Err_cmdViewJobs_Click:
MsgBox Err.Description
Resume Exit_cmdViewJobs_Click

End Sub

I keep getting the message box pop-up whether or not "cboCustomer" is null or not null. Can anyone spot the error in my code.

Thanks in advance.
 
Try something like...

Code:
Private Sub cmdViewJobs_Click()
On Error GoTo Err_cmdViewJobs_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmJob"

If Len(Nz(strCriteria, "")) Then
   MsgBox "You did not select anything from the list", vbExclamation
Else
   DoCmd.OpenForm stDocName, , , stLinkCriteria
End If
      
Exit_cmdViewJobs_Click:
    Exit Sub

Err_cmdViewJobs_Click:
    MsgBox Err.Description
    Resume Exit_cmdViewJobs_Click
    
End Sub

1) You can not find the length of a null variable, hence your error regarding Null. Use the Nz function.
2) Instead of using an early EXIT Sub, change the IF to an IF / ELSE statement.

Richard
 
Hi Willir,


Thanks, I tried your code & it doesn't seem to work. When the cmd button is clicked it opens "frmJob" regardless if "customer" is null or not null.

Do you have any other suggestions? I have been racking my brain for over a day on this!
 
Hi Willir,

I played around with the code a little:

Private Sub cmdViewJobs_Click()
On Error GoTo Err_cmdViewJobs_Click

Dim stDocName As String
stLinkCriteria = Forms!frmCardFile!cboCustomer <== I changed here


stDocName = "frmJob"

If Len(Nz(stLinkCriteria, "")) Then
MsgBox "You did not select anything from the list", vbExclamation
Else
DoCmd.OpenForm stDocName, , , stLinkCriteria
End If

Exit_cmdViewJobs_Click:
Exit Sub

Err_cmdViewJobs_Click:
MsgBox Err.Description
Resume Exit_cmdViewJobs_Click

End Sub


Now it works in reverse, which is still not correct. When I have nothing in the cboCustomer field it opens frmJob & when I select a customer it gives me the message box. This doesn't make any sense!!


 
Stupid me...
Len(Nz(stLinkCriteria, ""))
...returnd a number / true if there is text, just reverse the If / Else...

Code:
If Len(Nz(stLinkCriteria, "")) Then
   DoCmd.OpenForm stDocName, , , stLinkCriteria   
Else
   MsgBox "You did not select anything from the list", vbExclamation
End If
 
Absolutely legendary!!! That worked!

Thank you so much Willir!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top