0

I am experiencing a problem with vb in access. There is main form (say it parentForm), which has two buttons that trigger two different forms (say it childForm1 and childForm2). Both child forms perform checks on their Load events. If a condition fails, the childForm has to close. The problem is that in childForm1, the code works properly, in the childForm2 something goes completely wrong.

It seems that the close event is totally ignored. After the onLoad event, the process is carried out to the onCurrent event, which shouldn't happen! Below is the code of the onLoad event of childForm2.

Private Sub Form_Load()
On Error Resume Next

Dim db As Database
Dim rst As Recordset
Dim stDocName As String

stDocName = "childForm2"
closeEvent = False
Set db = CurrentDb

 If a<> 0 And b<> 0 Then
    Set rst = db.OpenRecordset("SELECT * FROM tbl1 WHERE Cust Like '*" & a & "*' AND Cust2 Like '*" & b & "*';")
    If (rst.EOF Or rst.BOF) And IsLoaded(stDocName) Then
       MsgBox ("No record found!")
       rst.Close
       SetWarnings = True
       closeEvent = True
       Me.SetFocus
       DoCmd.Close acForm, stDocName, acSaveNo
    End If
ElseIf a = 0 And b <> 0 Then
    Set rst = db.OpenRecordset("SELECT * FROM tbl1 WHERE Cust2 Like '*" & b & "*';")
    If (rst.EOF Or rst.BOF) Then
       MsgBox ("No record found!")
       rst.Close
       DoCmd.Close acForm, stDocName
    End If
End If
db.Close
End Sub

Also, I tried to use a global boolean variable (closeEvent in code), which is initialized to False and gets True, when the form must close. This variable is checked in the onCurrent event in order to close the form. However, when i debugged the code, the variable seems to lose its (True) value when passing from onLoad to OnCurrent event!

Any suggestion is more than appreciated.

Thanks in advance, Maria

1 Answer 1

2

Use the Form_Open event instead of the Form_Loadevent.

Then, instead of closing the form (=Docmd.Close) use the built in Cancel argument to cancel the form's opening.

Private Sub Form_Open(Cancel As Integer)

   If **condition not met** then
      Cancel = True  'this stops the form from opening
   End If

End Sub 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.