Run query based on populated fields

E

Emilio

Hello,
In a new record in a form, is there a way to run a query based on four
fields had been populated by the user.
Each field has to be populated before the query is run?
Is there a way to do this programatically?
 
E

Emilio

I need to run an append query into a table 1) when is a new record and 2)
after the user populates four fields in the form. The query picks up this
four fields from the fom to append the record into the table, however if one
of the fields is not populated it needs to let the user know that the field
has not been populated.
My questions are:
1) How do I determine if it is a new record? The query should not run if is
not a new record.
2) What would trigger the query to run? AfterUpdate? onDirty?
3) How can I make sure all the four fields have been populated?
thanks!!
 
G

Graham R Seach

Emilio,

<<How do I determine if it is a new record?>>
The form has a NewRecord property, which you can test:
If Me.NewRecord = False Then
'run the query
End

<<What would trigger the query to run?>>
Whatever trigger you like. I'd suggest the form's BeforeInsert or
AfterInsert events.

<<How can I make sure all the four fields have been populated?>>
Run code to make sure.
Private Sub Form_AfterInsert()
Dim sSQL As String

If (Me.NewRecord = False) Then
'Check that the 4 fields have been populated
If Not IsNull(Me.txtTextbox1) AND _
Not IsNull(Me.txtTextbox2) AND _
Not IsNull(Me.txtTextbox3) AND _
Not IsNull(Me.txtTextbox4) Then

sSQL = "INSERT INTO tblSomeTable.........."
DbEngine(0)(0).Execute sSQL, dbFailOnError
If (Err <> 0) Then
MsgBox Error " & Err.Number & vbCrLf & Err.Description
End If
Else
MsgBox "You must enter a value in the 4 textboxes"
End If
End
End Sub

If you give me the specifics, I can help you develop the SQL statement.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html
 
Top