Results 1 to 10 of 10
  1. #1
    ksmithson is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    7

    Reference Problem

    I am trying to validate records in a form by using the Before Update event in one of my subforms but when I put in the code, nothing happens. Do I need to reference the main form or subform?

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)If Me.cboxTeamWorkScore.ListIndex = -1 ThenMe.MsgBox "Please select a score."cboxTeamWorkScore.SetFocusCancel = TrueEnd IfIf Me.cboxReliabilityScore.ListIndex = -1 ThenMsgBox "Please select a score."Me.cboxReliabilityScore.SetFocusCancel = TrueEnd IfIf Me.cboxAdaptabilityWorkScore.ListIndex = -1 ThenMsgBox "Please select a score."Me.cboxAdaptabilityScore.SetFocusCancel = TrueEnd IfEnd Sub

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The SubFormBeforeUpdate event will occur when the SubForm record is Dirty and you either change to a different record in the SubForm or exit the SubForm back to the MainForm. Is that how you understand it? BTW it is much easier to read code if you surround it with the code tags (the # button when in advanced mode).

  3. #3
    ksmithson is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    7
    What I am trying to do is when the user goes to another record, the validation checks to see if all combo/text boxes in the sub forms are not null and if they are a message box pops up saying they have to fill in the nulls before they can move on to the next record. The code is in the sub form's Before Update event but nothing is happening and I am unsure if I have to reference the main form or something.

    Here is the cleaned up code:

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Me.cboxTeamWorkScore.ListIndex = -1 Then
    MsgBox "Please select a score."
    Me.cboxTeamWorkScore.SetFocus
    Cancel = True
    End If
    If Me.cboxReliabilityScore.ListIndex = -1 Then
    MsgBox "Please select a score."
    Me.cboxReliabilityScore.SetFocus
    Cancel = True
    End If
    If Me.cboxAdaptabilityWorkScore.ListIndex = -1 Then
    MsgBox "Please select a score."
    Me.cboxAdaptabilityScore.SetFocus
    Cancel = True
    End If
    End Sub

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Put a breakpoint at the beginning of the code and single step the code and you will see what is happening. If you hover over the variables the debugger will show you what they contain.
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
       If Me.cboxTeamWorkScore.ListIndex = -1 Then
          MsgBox "Please select a score."
          Me.cboxTeamWorkScore.SetFocus
          Cancel = True
       End If
       If Me.cboxReliabilityScore.ListIndex = -1 Then
          MsgBox "Please select a score."
          Me.cboxReliabilityScore.SetFocus
          Cancel = True
       End If
       If Me.cboxAdaptabilityWorkScore.ListIndex = -1 Then
          MsgBox "Please select a score."
          Me.cboxAdaptabilityScore.SetFocus
          Cancel = True
       End If
    End Sub

  5. #5
    ksmithson is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    7
    Well the code works now and I don't know why it fixed itself (I guess its my "Welcome to Access" initiaion/hazing) but can I be able to add the OR operator into the code so the message box does not pop up for each time one of the controls contain a null?

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Yes but you would still need to decide what control gets the focus next.

  7. #7
    ksmithson is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    7
    What way would be best to set the focus to the first control (set up from left to right) that has a null and then once the criteria is met the next null gets focused?

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Maybe you want to use the AfterUpdate event of each ComboBox.

  9. #9
    ksmithson is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    7
    You mean use the After Update event for each control and not for the subform itself?

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Nope. I would say use both.

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

Similar Threads

  1. Reference table
    By Newcomer in forum Access
    Replies: 6
    Last Post: 06-03-2010, 03:08 PM
  2. Reference to a Query
    By starhannes in forum Forms
    Replies: 5
    Last Post: 05-09-2010, 02:53 PM
  3. Dynamic fields reference
    By tuna in forum Forms
    Replies: 0
    Last Post: 05-04-2010, 11:47 PM
  4. Form Name Reference in VBA
    By Simon Sweet in forum Programming
    Replies: 0
    Last Post: 05-22-2008, 01:55 PM
  5. How to add Opensource reference???
    By loui in forum Access
    Replies: 0
    Last Post: 10-04-2007, 04:24 AM

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