If two conditions are true

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

Guest

I need to build a trap into a form, whereby if Field1 = "Keyword" and Field2
is null then a message box pops up saying that Field2 needs to be filled in.
A potential problem is that the person who made this database designed a
printable form (not report) with unbound text boxes, and code in a command
button's Click event along the lines of:
Set rst = New ADODB.Recordset
rst.Open "maintable", cnn, adLockOptimistic, adOpenDynamic
rst.AddNew
rst![Field1] = Me.txtField1
etc. (txtField1 is a text box with Field1 as its control source). Field1
has no value until after the referenced code has run, so adding a trap before
then would have to reference the text box, I suppose.
I would not have taken this course with the database, but I do not have time
to do more than patch it for now. That being said, any suggestions?
 
you can run the trap before the ADO code, as

If Me.txtField1 = "Keyword" and IsNull(Me.txtField2) Then
MsgBox "Enter a value in txtField2, please."
End Sub
End If

only thing confused me a little is that you said the form has unbound text
boxes, then said that "txtField1 is a text box with Field1 as its control
source". the above code assumes that both controls are unbound, with no
values entered unless the user does it, before clicking the command button.

hth
 
Hi I am not sure where you want the trap but if it is on the button click
event then just put in a IF
statement before hand
This is air code but you should get the idea

If Me!Field1.Value = "Keyword" And IsNull(Me!Field2) Then
MsgBox "You must enter a value in Field2", vbOKOnly, Missing Data"
Else
rst.Open "maintable", cnn, adLockOptimistic, adOpenDynamic
rst.AddNew
rst![Field1] = Me.txtField1
EndIf

Bruce
 
Thanks to both for the help. Turns out I was on the right general track, but
had the syntax wrong. I meant to say that Field1 is the destination for the
contents of txtField1 after the code is run, which is a bit different than
being the control source. Sorry about any confusion that caused. The code
runs when a Save and Print command button is clicked. Some of the fields in
the form are filled in automatically based on fields (part number, manual
number, etc.) from the table on which the form is based. One of these fields
is the first field in the If statement. The user fills in other fields (job
number, quantity, etc.), which are saved to another table in the code I
mentioned in my original post. The unbound text box, the contents of which
will eventually be saved to this second table, is referenced after the And
part of the line.
I added a GoTo line (and a SetFocus line to direct the cursor to the field
that needs to be filled in) after the MsgBox line in order to jump past the
part of the code that saves the data, because otherwise after clicking OK the
rest of the code would run anyhow. It would have helped if I had mentioned
that the code runs from a command button that also prints the form. My brain
was not at its most efficient at the end of the day when I posted my question.
Thanks again.


fdd said:
Hi I am not sure where you want the trap but if it is on the button click
event then just put in a IF
statement before hand
This is air code but you should get the idea

If Me!Field1.Value = "Keyword" And IsNull(Me!Field2) Then
MsgBox "You must enter a value in Field2", vbOKOnly, Missing Data"
Else
rst.Open "maintable", cnn, adLockOptimistic, adOpenDynamic
rst.AddNew
rst![Field1] = Me.txtField1
EndIf

Bruce


Bruce said:
I need to build a trap into a form, whereby if Field1 = "Keyword" and
Field2
is null then a message box pops up saying that Field2 needs to be filled
in.
A potential problem is that the person who made this database designed a
printable form (not report) with unbound text boxes, and code in a command
button's Click event along the lines of:
Set rst = New ADODB.Recordset
rst.Open "maintable", cnn, adLockOptimistic, adOpenDynamic
rst.AddNew
rst![Field1] = Me.txtField1
etc. (txtField1 is a text box with Field1 as its control source). Field1
has no value until after the referenced code has run, so adding a trap
before
then would have to reference the text box, I suppose.
I would not have taken this course with the database, but I do not have
time
to do more than patch it for now. That being said, any suggestions?
 
Back
Top