Results 1 to 7 of 7
  1. #1
    cfobare79 is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Dec 2014
    Posts
    66

    VBA Coding Help please

    I have this code below that I have put together. I don't know much about coding but need to ensure Text boxes are filled out and if they are filled out then close window and open form "LogIn". Currently the text boxes are verified but if they are not complete I get my message box which you hit ok on and it just opens up the "LogIn" page without allowing you to finish filling out the current form "Customer Form".

    Private Sub ShoppingCart_Click()
    Dim msg As String, Style As Integer, Title As String
    Dim nl As String, ctl As Control




    nl = vbNewLine & vbNewLine


    For Each ctl In Me.Controls
    If ctl.ControlType = acTextBox Then
    If ctl.Tag = "*" And Trim(ctl & "") = "" Then
    msg = "Data Required for '" & ctl.Name & "' field!" & nl & _
    "You can't save this record until this data is provided!" & nl & _
    "Enter the data and try again . . . "
    Style = vbCritical + vbOKOnly
    Title = "Required Data..."


    MsgBox msg, Style, Title
    ctl.SetFocus
    Cancel = True
    DoCmd.BrowseTo acBrowseToForm, "LogIn"
    Exit For

    End If
    Else

    End If

    Next

    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,948
    Cancel = True means nothing in the Click event. This is a feature of BeforeUpdate event. Need an Else branch

    If ... Then
    'msgbox code
    Else
    DoCmd.BrowseTo acBrowseToForm, "LogIn"
    End If

    I've never used BrowseTo, only OpenForm. Is this a web db?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    cfobare79 is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Dec 2014
    Posts
    66
    This is not a web db. The BrowseTo function does both the open new window and close current window functions as one code. I have been doing a lot of research but I am still very new to code so I'm not sure exactly how to implement your comment above. Can you explain in a little more detail the "If...Then 'msgbox code" part?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,948
    I was being lazy and not replicating your code - modify your code as suggested. I just noticed the Else branch in your code, move it above the BrowseTo line. The Exit For and Cancel are not needed.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    cfobare79 is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Dec 2014
    Posts
    66
    With the code below nothing is happening and the "End Sub" is highlighted yellow. I really have little to no clue what I'm doing so the more detail the better.


    Private Sub ShoppingCart_Click()
    Dim msg As String, Style As Integer, Title As String
    Dim nl As String, ctl As Control




    nl = vbNewLine & vbNewLine


    For Each ctl In Me.Controls
    If ctl.ControlType = acTextBox Then
    If ctl.Tag = "*" And Trim(ctl & "") = "" Then
    msg = "Data Required for '" & ctl.Name & "' field!" & nl & _
    "You can't save this record until this data is provided!" & nl & _
    "Enter the data and try again . . . "
    Style = vbCritical + vbOKOnly
    Title = "Required Data..."
    MsgBox msg, Style, Title
    ctl.SetFocus
    Cancel = True
    Else
    DoCmd.BrowseTo acBrowseToForm, "LogIn"
    End If
    Else


    End If

    Next

    End Sub

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,948
    Sorry, I was wrong about the Exit For, it is useful. Also, think you need a Boolean and the Else actually is not needed.

    Code:
    Dim booContinue As Boolean
    booContinue = True
    For Each ctl In Me.Controls
         If ctl.ControlType = acTextBox Then
            If ctl.Tag = "*" And Trim(ctl & "") = "" Then
                   msg = "Data Required for '" & ctl.Name & "' field!" & nl & _
                   "You can't save this record until this data is provided!" & nl & _
                           "Enter the data and try again . . . "
                   Style = vbCritical + vbOKOnly
                   Title = "Required Data..."
                   MsgBox msg, Style, Title
                   ctl.SetFocus
                   booContinue = False
                   Exit For
             End If
         End If
    Next
    If booContinue Then DoCmd.BrowseTo acBrowseToForm, "LogIn"
     End Sub
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    cfobare79 is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Dec 2014
    Posts
    66
    You are wonderful... I can't thank you enough for taking time out of your Sunday to fix my code.

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

Similar Threads

  1. help with coding please
    By alexandervj in forum Access
    Replies: 2
    Last Post: 02-11-2014, 02:45 PM
  2. Coding for Sum in vba
    By glen in forum Programming
    Replies: 14
    Last Post: 12-20-2012, 05:10 PM
  3. Please help with VB coding
    By winterh in forum Import/Export Data
    Replies: 11
    Last Post: 03-19-2012, 06:05 PM
  4. Need help in VBA coding
    By Kcgp in forum Programming
    Replies: 6
    Last Post: 02-01-2012, 11:22 PM
  5. Bar-Coding
    By texasprincess7 in forum Access
    Replies: 1
    Last Post: 02-12-2009, 10:29 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