Pesky button issue

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Greetings,

I am currently using Access 2000 to run a document image database and have
run into a bit of a issue with coding in some buttons.


For the most part, starting with a switchboard menu I want to open a form
running off a query to search records. My goal is if No records are found,
the code takes me into the form with a new record. If cancel is clicked, I
want to go back out to the previous switchboard.

The first batch of code is what is running so far. But there are 2 things.
First, for some reason, coding the vbCancel button to open a new record
causes the OK button to do this, and vice versa which seems backwards to what
I am trying to write.

Second, in trying to write a second If..Then for the actual Cancel button,
it simply passes me into the form, but I want to go back out to the
switchboard. I pasted what I wrote in my attempts at that, as the "Second
try." Any help would be appreciated.

Code running so far:
Private Sub Form_Open(Cancel As Integer)
Dim stDocName As String
Dim strMessage As String
Dim intOptions As Integer
Dim bytChoice As Byte

stDocName = "frmPerCustomer"
If Me.Recordset.RecordCount = 0 Then
strMessage = "Job Not found, click OK to Continue."
intOptions = vbQuestion + vbOKCancel
bytChoice = MsgBox(strMessage, intOptions)

If bytChoice = vbCancel Then
DoCmd.Close
DoCmd.OpenForm stDocName, acNewRecord
End If

End If
End Sub

Second try:

Private Sub Form_Open(Cancel As Integer)
Dim stDocName As String
Dim strMessage As String
Dim intOptions As Integer
Dim bytChoice As Byte

stDocName = "frmPerCustomer"
If Me.Recordset.RecordCount = 0 Then
strMessage = "Job Not found, click OK to Continue."
intOptions = vbQuestion + vbOKCancel
bytChoice = MsgBox(strMessage, intOptions)

If bytChoice = vbOK Then
DoCmd.Close
DoCmd.OpenForm stDocName, acNewRecord
End If

If bytChoice = vbCancel Then
DoCmd.Close
DoCmd.OpenForm "frmSearchMenu", acNormal
End If
End Sub
 
Greetings,

Just an update, I altered the code just a bit. Now the Cancel button works
as it should, but the OK button now tries to run the query the form is based
on. The code is written in the "On Open" event. Again, I'm almost there and
would appreciate nay nudge in the right direction.

Cheers,
Brad

My revised code:

Private Sub Form_Open(Cancel As Integer)
Dim stDocName As String
Dim strMessage As String
Dim intOptions As Integer
Dim bytChoice As Byte

stDocName = "frmPerCustomer"
If Me.Recordset.RecordCount = 0 Then
strMessage = "Job Not found, click OK to Continue."
intOptions = vbQuestion + vbOKCancel
bytChoice = MsgBox(strMessage, intOptions)

If bytChoice = vbOK Then
DoCmd.Close
DoCmd.OpenForm stDocName, acNewRecord
End If

If bytChoice = vbCancel Then
DoCmd.Close
DoCmd.OpenForm "frmSearchMenu", acNormal
End If
End If

End Sub
 
Greetings,

A big nevermind on this one. After writing the post, I walked outside
thinking the problem through me head, especially since the query seemed to be
running itself twice. It dawned on me the in the section:

If bytChoice = vbOK Then
DoCmd.Close
DoCmd.OpenForm stDocName, acNewRecord
End If

I was closing the form and re-opening it, causing the query to run a second
time, simply removing the DoCmd.Close caused everything to work the way I
planned it.


This is what I wound up with, whiched work for anyone else who may need it:

Private Sub Form_Open(Cancel As Integer)
Dim stDocName As String
Dim strMessage As String
Dim intOptions As Integer
Dim bytChoice As Byte

stDocName = "frmPerCustomer"
If Me.Recordset.RecordCount = 0 Then
strMessage = "Job Not found, click OK to Continue."
intOptions = vbQuestion + vbOKCancel
bytChoice = MsgBox(strMessage, intOptions)

If bytChoice = vbOK Then
DoCmd.OpenForm stDocName, acNewRecord
End If

If bytChoice = vbCancel Then
DoCmd.Close
DoCmd.OpenForm "frmSearchMenu", acNormal
End If
End If

End Sub

Cheers,
Brad
 
Back
Top