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!

Updating Field Value from Checkbox 1

Status
Not open for further replies.

MontgomeryPete

Instructor
Apr 3, 2004
57
US
I have a checkbox on a form that is used to indicate that an item has been dispatched. We want to update a field in a table with the time when the box is checked. I have the code in an event session "On Click."

Private Sub Form_Click()
Update Ride, TimeDispatched
SET TimeDispatched =
IIf
([Ride].[Dispatched]= -1,
[Ride.TimeDispatched] = Time()
End Sub

The code fails, and would appreciate some help on how to execute this subroutine.

Thanks, Pete

 
Maybe you can put it in the before-insert event.

Pampers [afro]
Keeping it simple can be complicated
 
How are ya MontgomeryPete . . .

The code should reside in the [blue]AfterUpdate[/blue] event of the combobox and should look like:
Code:
[blue]   Dim SQL As String
   
   SQL = "UPDATE Ride " & _
         "SET [TimeDispatched] = " & Time() & ";"
   DoCmd.RunSQL SQL[/blue]
However be aware, because there's [blue]no criteria in the SQL you provided[/blue] you'll update all records! I don't believe this is what you wanted. If I'm right, then theSQL changes to:
Code:
[blue]   SQL = "UPDATE Ride " & _
         "SET [TimeDispatched] =" & Time() & " " & _
         "WHERE ([[purple][b]PrimaryKeyName[/b][/purple]] = [red][b]'[/b][/red]" & Me![purple][b]PrimaryKeyName[/b][/purple] & "[red][b]'[/b][/red]);"
   DoCmd.RunSQL SQL[/blue]
Here the [purple]PrimaryKey[/purple] of the current record is used to ping the actual record to change in the table. Note: if the primarykey is text, remove the single quotes in [red]red[/red].

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .
 
MontgomeryPete . . .

You need to consider what happens [blue]when a user unchecks the checkbox[/blue] (checkbox set by mistake)!

Calvin.gif
See Ya! . . . . . .
 
Thanks for the additional thoughts. We will have to set the field back to null. Naturally, when I asked the user, he said "Oh, sure, we change that all the time."

Thanks again, Pete

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top