Page 1 of 3 123 LastLast
Results 1 to 15 of 40
  1. #1
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93

    Still having trouble with DoCmd.RunSQL

    Misc Screen Shots.docOnClick Event on Save & Exit Button on form frmFormerEmployerEntry:

    Code:
    Private Sub btnSaveAndExit_Click()
        'Make an entry into the Former Employers Table and exit the form
        DoCmd.RunSQL "insert into [tblFormerEmployers] ([numApplID],[txtEmployerName],[txtFmrEmployerPhone],[dteDateFrom],[dteDateTo],[curSalary],[txtSalaryUnit],[txtPosition],[txtLeavingReason]) values(" & numApplicantID & ", txtFldEmployerName,txtFldFmrEmplPhone, dteFldStartDate, dteFldEndDate, curFldOldSalary, cboPayUnits, txtFldPosition,txtFldReasonForLeaving);"
        
        
        With Me
            .txtFldEmployerName = ""
            .txtFldAddreess1 = ""
            .txtFldAddreess2 = ""
            .txtFldCity = ""
            .txtFldState = ""
            .txtFldZipCode = ""
            .txtFldCountry = ""
            .txtFldPostalCode = ""
            .txtFldFmrEmplPhone = ""
            .dteFldStartDate = ""
            .dteFldEndDate = ""
            .curFldOldSalary = 0
            .txtFldPosition = ""
            .txtFldReasonForLeaving = ""
        
        End With
        
        DoCmd.Close acForm, "frmFormerEmployerEntry"
        Forms![frmEmploymentApp].Requery
    
    
    End Sub

    This works perfectly. However, I have more than one Sub that uses all but the last two lines so I want to reuse that code instead of maintaining two copies.

    In a Global module "Reused Code":

    Code:
    Sub AppendEmployer(frm As Form)
        
        'Make an entry into the Former Employers Table
        DoCmd.RunSQL "insert into [tblFormerEmployers] ([numApplID],[txtEmployerName],[txtFmrEmployerPhone],[dteDateFrom],[dteDateTo],[curSalary],[txtSalaryUnit],[txtPosition],[txtLeavingReason]) values(" & numApplicantID & ", frm.txtFldEmployerName, frm.txtFldFmrEmplPhone, frm.dteFldStartDate, frm.dteFldEndDate, frm.curFldOldSalary, frm.cboPayUnits, frm.txtFldPosition,frm.txtFldReasonForLeaving);"
        
        With frm
            .txtFldEmployerName = ""
            .txtFldAddreess1 = ""
            .txtFldAddreess2 = ""
            .txtFldCity = ""
            .txtFldState = ""
            .txtFldZipCode = ""
            .txtFldCountry = ""
            .txtFldPostalCode = ""
            .txtFldFmrEmplPhone = ""
            .dteFldStartDate = ""
            .dteFldEndDate = ""
            .curFldOldSalary = 0
            .txtFldPosition = ""
            .txtFldReasonForLeaving = ""
        
        End With
    
    
    End Sub

    Changed Original Sub to:



    Code:
    Private Sub btnSaveAndExit_Click()
    
    
        Call AppendEmployer(Me)
    
    
        DoCmd.Close acForm, "frmFormerEmployerEntry"
        Forms![frmEmploymentApp].Requery
    
    
    End Sub



    It crashes at DoCmd.RunSQL "insert into [tblFormerEmployers] ([numApplID],[txtEmployerName],[txtFmrEmployerPhone],[dteDateFrom],[dteDateTo],[curSalary],[txtSalaryUnit],[txtPosition],[txtLeavingReason]) values(" & numApplicantID & ", frm.txtFldEmployerName, frm.txtFldFmrEmplPhone, frm.dteFldStartDate, frm.dteFldEndDate, frm.curFldOldSalary, frm.cboPayUnits, frm.txtFldPosition,frm.txtFldReasonForLeaving);"


    Not knowing what frm.txtFldEmployerName is, even though my Add Watch shows it ("El Pollo Loco") as plain as day. See attachment.




  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    No attachment?
    Also what do you mean by 'it crashes'? What error do you get?
    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

  3. #3
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    I attempted to attach a Word document with a screenshot of the actual message. It must not have happened. The exact response was a dialog box that prompts "Enter Parameter Value". It references "frmtxtFldEmployerName". I'll try again to attach that Word Doc. It is now attached.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    I cannot see why TBH.
    I would put a few debug.prints in to see values, or set breakpoint and use the immediate window.
    I would put the sql into a string variable and debug.print that.

    The only way I can see to get around this is to concatenate the values as you do for the first value?
    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
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    Quote Originally Posted by Welshgasman View Post
    I cannot see why TBH.
    I would put a few debug.prints in to see values, or set breakpoint and use the immediate window.
    I would put the sql into a string variable and debug.print that.

    The only way I can see to get around this is to concatenate the values as you do for the first value?
    Yeah no sweat, appreciate your time. I'll poke at this during the weekend.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    You have to concatenate each variable into the string separately. More on delimiters here:

    https://www.baldyweb.com/BuildSQL.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    Like I said earlier, the "concatenation" was accepted as part of an OnClick Event on a form. But simply by virtue of moving the exact same concatenated string to its own module, it is somehow now in error, THIS is what I mean by nonintuitive. This is off topic but in another project I was stymied by a change in a query that was not performing a expected. After hours of research I figured out that though the Design View showed the correctly changed query, the SQL View showed the unchanged query. I had to physically modify the SQL View before the query would work! Any more it seems I have to take Microsoft errors with a grain of salt. Is it REALLY a syntax or logic error or is it an undocumented Microsoft "feature"? It makes coding more stressful when you have to question your own tool.

    But thanks for the link. I will pore over it this weekend.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Do you have an example you can post of this working?

    Code:
    insert into [tblFormerEmployers] ([numApplID],[txtEmployerName],[txtFmrEmployerPhone],[dteDateFrom],[dteDateTo],[curSalary],[txtSalaryUnit],[txtPosition],[txtLeavingReason]) values(" & numApplicantID & ", txtFldEmployerName,txtFldFmrEmplPhone, dteFldStartDate, dteFldEndDate, curFldOldSalary, cboPayUnits, txtFldPosition,txtFldReasonForLeaving);"
    I can't imagine that concatenation working. In my experience each value must be concatenated separately.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    My very first post, where I said it was working perfectly.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I understand, I was wondering if you could attach a db here showing it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    VBA that does not concatenate reference to form controls to build SQL works when using DoCmd.RunSQL not CurrentDb.Execute. VBA finds the controls when it constructs the SQL and pulls their values.

    Will not work in a general module unless you use full form reference with each control.

    DoCmd.RunSQL "INSERT INTO Tablename(FieldName) VALUES(Forms!Form1.Text1)"

    Otherwise, concatenate each frm.control reference to use the frm object variable.

    DoCmd.RunSQL "INSERT INTO Tablename(FieldName) VALUES(" & frm.controlname & ")"
    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
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    Sorry, can't easily. Data is pulled from linked Actian Zen MySQL tables.

  13. #13
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    Quote Originally Posted by June7 View Post
    VBA that does not concatenate reference to form controls to build SQL works when using DoCmd.RunSQL not CurrentDb.Execute. VBA finds the controls when it constructs the SQL and pulls their values.

    Will not work in a general module unless you use full form reference with each control.

    DoCmd.RunSQL "INSERT INTO Tablename(FieldName) VALUES(Forms!Form1.Text1)"

    Otherwise, concatenate each frm.control reference to use the frm object variable.

    DoCmd.RunSQL "INSERT INTO Tablename(FieldName) VALUES(" & frm.controlname & ")"

    Hi,

    Why is your syntax different from mine? Why no square brackets? I tried it that way; it still crashed, but with a different error.

    For multiple values, what to do with the commas? Maybe like this? DoCmd.RunSQL "INSERT INTO Tablename(FieldName1, Fieldname2) VALUES(" & frm.controlname & "," & frmcontrolname2 & ")"

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Square brackets are only needed when names have spaces or special characters (other than underscore) or are reserved words (like Date).

    What error?

    I have not tested using a passed form object variable. I presume concatenation will work with properly structured code.

    Yes, commas within quotes.

    If field is text type, use apostrophe delimiters, if date/time use # delimiters. I prefer CurrentDb.Execute, like:

    CurrentDb.Execute "INSERT INTO Tablename(FieldName1, FieldName2) VALUES('" & frm.controlname & "', #" & frm.controlname2 & "#)"
    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.

  15. #15
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Good catch June7, I hadn't considered that RunSQL could resolve the form references. I wouldn't do it that way, but interesting none the less.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 44
    Last Post: 01-04-2021, 02:57 PM
  2. my first DoCmd.RunSQL
    By broecher in forum Programming
    Replies: 4
    Last Post: 11-05-2010, 09:35 PM
  3. docmd.runsql update
    By Mitch_Aus in forum Programming
    Replies: 2
    Last Post: 10-05-2010, 09:45 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