Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Allie12380 is offline Novice
    Windows XP Access 2016
    Join Date
    Dec 2024
    Posts
    12

    Log table - DoCmd.RunSQL writing data as 2 rows instead of 1

    Goal: Use code below to write one row of data to log table when form is opened that captures the user and form name.
    Current state: Code writes the data to table but it inserts it into the tbl_Log as two rows. One with username and one with form name.
    Question: How can I edit the code below to write the UserName and fSTampOpen onto the same row? Also, what is right indenting for this code to space it out?


    Private Sub Form_Load()
    'Loads the user text box on frmSwitch Main and logs event in tbl Log
    'Loads the form name text box on frm


    Me![txtUser] = NetUser()
    DoCmd.SetWarnings False
    Me![txtformname] = GetActiveFormName()
    DoCmd.SetWarnings False
    DoCmd.RunSQL "Insert Into tbl_Log (UserName) Values (Forms!frmSwitch_Main!txtUser);"
    DoCmd.SetWarnings False
    DoCmd.RunSQL "Insert Into tbl_Log (fStampOpen) Values (Forms!frmSwitch_Main!txtformname);"
    DoCmd.SetWarnings False
    End Sub

    Here is example of how data is currently writing to the table that shows the 2 rows:

  2. #2
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Use one INSERT statement with both fields/values
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  3. #3
    Allie12380 is offline Novice
    Windows XP Access 2016
    Join Date
    Dec 2024
    Posts
    12
    I tried this as one but only get user name returned. The one insert row works in SQL database I have but cant get it to work in Access.

    Private Sub Form_Load()
    'Loads the user text box on frmSwitch Main and logs event in tbl Log
    'Loads the form name text box on frm
    Me![txtUser] = NetUser()
    DoCmd.SetWarnings False
    Me![txtformname] = GetActiveFormName()
    DoCmd.SetWarnings False
    DoCmd.RunSQL "Insert Into tbl_Log (UserName,fStampOpen) Values (Forms!frmSwitch_Main!txtUser,Forms!frmSwitch_Main !txtformname);"
    'DoCmd.SetWarnings False
    'DoCmd.RunSQL "Insert Into tbl_Log (fStampOpen) Values (Forms!frmSwitch_Main!txtformname);"
    DoCmd.SetWarnings False
    End Sub

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,553
    That is because you are only supplying one value?

    And please start using code tags.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Allie12380 is offline Novice
    Windows XP Access 2016
    Join Date
    Dec 2024
    Posts
    12
    How do I supply the 2nd value?

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,553
    Code:
    DoCmd.RunSQL "Insert Into tbl_Log (UserName,fStampOpen) Values (Forms!frmSwitch_Main!txtUser,Forms!frmSwitch_Main!txtformname);"
    When you do not know the syntax, look it up.

    Actually, now I have put it into code tags, I can see you do actually have two values in there.

    Have you walked your code to see what those values actually are, and not what you think they are?
    Put a Debug.Print in for both those values, BEFORE you try to use them.

    Tip: If that code is in Forms!frmSwitch_Main. then I would use Me.txtUser etc.
    Only use full name when you have no other choice.

    You had a space in the second name. Now this site sometimes adds a space itself, but if you really have that space, then it would appear you do not have Option Explicit at the top of your modules. That would highlight the fact that that control does not exist.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Side note: need DoCmd.SetWarnings True after running SQL.

    Welsh, OP has embedded control references between quotes. Works with RunSQL but requires full path reference - Me. won't work unless they do concatenation.
    Option Explicit would not catch space in that string.

    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.

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,553
    Quote Originally Posted by June7 View Post
    Side note: need DoCmd.SetWarnings True after running SQL.

    Welsh, OP has embedded control references between quotes. Works with RunSQL but requires full path reference - Me. won't work unless they do concatenation.

    Ah, my mistake. That is the way I normally do it when needed, and missed that important point.
    Sorry @allie12380
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    If you want to provide db for analysis, follow instructions at bottom of my post.

    What "SQL database"? Is this still the backend?

    Do you show user and formname in texboxes on every form?
    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.

  10. #10
    Allie12380 is offline Novice
    Windows XP Access 2016
    Join Date
    Dec 2024
    Posts
    12
    Welshgasman and June7!!!!
    Thank you both
    Two changes that got it working:
    1) Updated the DoCmd.SetWarnings True after running SQL. I had False originally.
    2) In the declare variable part "
    Values (Forms!frmSwitch_Main!txtUser,Forms!frmSwitch_Main !txtformname);" changed to (Forms!frmSwitch_Main![txtUser],Forms!frmSwitch_Main![txtformname]). Just added brackets around the field name.

    Now it all works. Would not have figured this out without both of you.

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Well, those are textbox names, not field names.
    Brackets should not be needed. Works for me without.
    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.

  12. #12
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Although you now have a solution, something like the following should also work and is much neater

    Code:
    Private Sub Form_Load()
         'Loads the user text box on frmSwitch Main and logs event in tbl Log
          Me.txtUser = NetUser()
    
         
         'Loads the form name text box on frm
          Me.txtformname = GetActiveFormName()
    
         'Add values to table
         CurrentDb.Execute "Insert Into tbl_Log (UserName, fStampOpen) Values ('" & NetUser() & "', '" & GetActiveFormName() & "');", dbFailOnError
    
    
    End Sub
    The INSERT statement now uses the same functions already used to populate the 2 textboxes
    Using CurrentDb.Execute means no SetWarnings statements are needed.
    Using dbFailOnError means messages still appear in case of code errors

    It may be possible to streamline this still further!

  13. #13
    Allie12380 is offline Novice
    Windows XP Access 2016
    Join Date
    Dec 2024
    Posts
    12
    Thank you! Appreciate the explanations on the code elements too.

  14. #14
    Allie12380 is offline Novice
    Windows XP Access 2016
    Join Date
    Dec 2024
    Posts
    12
    I am not sure why in this database needed the brackets. Removed them and tried it and wont work.

  15. #15
    Allie12380 is offline Novice
    Windows XP Access 2016
    Join Date
    Dec 2024
    Posts
    12
    updated to this code and it works perfect and faster than the non-streamlined code

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 02-24-2014, 09:26 AM
  2. Replies: 3
    Last Post: 02-05-2013, 05:07 PM
  3. Replies: 2
    Last Post: 05-16-2012, 07:46 AM
  4. Problem with DoCmd.RunSQL
    By Peter01 in forum Programming
    Replies: 2
    Last Post: 09-10-2009, 07:11 PM
  5. docmd.runsql not working
    By Rameez in forum Programming
    Replies: 3
    Last Post: 08-07-2009, 10:07 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