Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2009
    Posts
    6

    Toggle checkbox

    Hi, Hopeosmeone could help me with this.



    I have a application where by when a number is entered (using text box on a form at the moment) it retrieves a record from the form, and I want it to toggle a checkbox.

    If I enter the number, and the box is checked, I want to uncheck, and visa versa (so if not checked I want it to check it)

    The entered text will come from a hand scanner. So, I scan a barcode, and I want the application to automatically check/uncheck the box depending on its current state, but only for the record corresponding to the number entered.

    Code I have so far is:

    Public Function onoffyes()
    Dim strOnoffyes As String
    strOnoffyes = "UPDATE crew " & _
    "SET crew.onoff = yes " & _
    "WHERE crew.ID = " & Forms!crew!Barcode
    DoCmd.RunSQL strOnoffyes
    End Function

    Public Function onoffno()
    Dim strOnoffno As String
    strOnoffno = "UPDATE crew " & _
    "SET crew.onoff = yes " & _
    "WHERE crew.ID = " & Forms!crew!Barcode
    DoCmd.RunSQL strOnoffno
    End Function

    Private Sub Barcode_lostfocus()
    Dim varOnoff As String

    varOnoff = DLookup("onoff", "crew", "ID = " & Me.Barcode)

    If varOnoff = no Then
    Call onoffno
    ElseIf varOnoff = yes Then
    Call onoffyes
    End If



    Me.Refresh

    End Sub

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Is the Crew form bound to the Crew table and do you display the [onoff] field?

  3. #3
    Join Date
    Apr 2009
    Posts
    6
    yes and yes. The onoff field is a checkbox formatted as yes/no. it is displayed in the body. I want to run the procedure when the textbox looses focus.

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    You do not need all of the complicated code. Just put this in the AfterUpdate event of the TextBox:
    Me.CheckBox = Not Me.CheckBox
    ...using your control names of course.

  5. #5
    Join Date
    Apr 2009
    Posts
    6
    ok, I can get the checkbox to toggle now, but only for the first record.

    The form I have lists all records from a table.

    The part I am stuck on is that I only want my checkbox to toggle where the data entered in the textbox equals the ID for that row (The row contains ID (key), surname, firstname and the checkbox)

    The checkbox toggles only the first row at the moment?

    I am thinking some sort of query or create a recordset?

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    So you want code like:
    Code:
    If Me.TextBox = Me.ID Then
       Me.CheckBox = Not Me.CheckBox
    End If
    Does your form display more than one record at a time? Is it in Datasheet or Continuous Form mode?

  7. #7
    Join Date
    Apr 2009
    Posts
    6
    All records are listed like when you would select Tabular using the wizard.

    (I did not build the database originally, just trying to add functionality)

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    What we have set up now will only change the CheckBox when a new value is entered into the TextBox. Are you trying to go through the entire table and set the CheckBox to some value when the TextBox matches the ID field?

  9. #9
    Join Date
    Apr 2009
    Posts
    6
    yes, exactley.

    So with what you have told me so far I can toggle the checkbox but only for the first record.

    Displayed on the form are all the records, and I need to add some sort of lookup (I think?) so the checkbox value only changes when the textbox matches the ID for the row.

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I guess you will need to describe your table a bit more for me to understand what you are doing. I thought you only needed to change one record. What does the toggle CheckBox field represent?

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Checkbox
    By Rbtsmith in forum Access
    Replies: 2
    Last Post: 02-17-2009, 04:19 PM
  2. checkbox
    By Suresh in forum Forms
    Replies: 0
    Last Post: 12-19-2007, 01:30 AM
  3. easy checkbox question
    By nelsok in forum Forms
    Replies: 1
    Last Post: 06-09-2006, 05:46 PM
  4. Checkbox to update value
    By Wrangler in forum Forms
    Replies: 1
    Last Post: 01-03-2006, 06:34 PM
  5. access checkbox
    By nshack31 in forum Forms
    Replies: 1
    Last Post: 12-09-2005, 10:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums