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

    OK I will try the various suggestions and get back to you guys this evening.

  2. #17
    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
    Square brackets are only needed when names have spaces or special characters (other than underscore) or are reserved words (like Date).

    What error?
    Please see attachment. When I run my code as-is, I get my aforementioned error. When I change the concatenation to what was recommended, I now get an "Enter Parameter" prompt that lists the content of the control as a parameter! I have a text box whose full reference in my instance is frm.txtFldEmployerName. Its current contents is "Paul". It is now asking me for the value of a "Parameter" called "Paul". WTH, man?!

    DoCmd-RunSQL Bugs.doc

  3. #18
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by pledbetter View Post
    Please see attachment. When I run my code as-is, I get my aforementioned error. When I change the concatenation to what was recommended, I now get an "Enter Parameter" prompt that lists the content of the control as a parameter! I have a text box whose full reference in my instance is frm.txtFldEmployerName. Its current contents is "Paul". It is now asking me for the value of a "Parameter" called "Paul". WTH, man?!

    DoCmd-RunSQL Bugs.doc
    This is your code
    Code:
    When I change the “concatenation” as recommended: … values(" & numApplicantID & "," & frm.txtFldEmployerName & ",… <and so on>…
    Put it all in a string variable, then you can debug.print it.
    Paul would need to be 'Paul' as it is a string. Without those, then Access thinks it is a variable, which it could well be. That is why you get the prompt.

    Strings need to be surrounded by single quotes ' unless it contains a single quote, then triple double quotes works, I think?
    Date literals with # and in mm/dd/yyyy or yyyy-mm-dd format
    Numbers do not need anything

    Also for anything other than one item of criteria, I tend to put the the criteria into a string variable and then debug.print it, until correct then also use that in the code.
    Added benefit is, if you still cannot see the error, you can copy and paste back here the output from the debug.print for someone else to spot it.
    Example:
    tt="Eg'g"
    ? dlookup("FoodID","tblFoods","FoodName = """ & tt & """")

    Look up the syntax when you cannot get it to work.

    https://learn.microsoft.com/en-us/of...oft-access-sql
    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

  4. #19
    Minty is online now VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    It might be worth having a quick read here:
    https://medium.com/@NoLongerSet/avoi...s-c036348dd732
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  5. #20
    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
    This is your code
    Code:
    When I change the “concatenation” as recommended: … values(" & numApplicantID & "," & frm.txtFldEmployerName & ",… <and so on>…
    Put it all in a string variable, then you can debug.print it.
    Paul would need to be 'Paul' as it is a string. Without those, then Access thinks it is a variable, which it could well be. That is why you get the prompt.

    Strings need to be surrounded by single quotes ' unless it contains a single quote, then triple double quotes works, I think?
    Date literals with # and in mm/dd/yyyy or yyyy-mm-dd format
    Numbers do not need anything

    Also for anything other than one item of criteria, I tend to put the the criteria into a string variable and then debug.print it, until correct then also use that in the code.
    Added benefit is, if you still cannot see the error, you can copy and paste back here the output from the debug.print for someone else to spot it.
    Example:
    tt="Eg'g"
    ? dlookup("FoodID","tblFoods","FoodName = """ & tt & """")

    Look up the syntax when you cannot get it to work.

    https://learn.microsoft.com/en-us/of...oft-access-sql
    Again. My original syntax is correct. Running my original code in a OnClick event on a button, it works perfectly. I fact, I have TWO buttons that run almost the identical code; one does the append and exits, and the other appends and re-presents a form to collect more data to be uploaded. By virtue of simply transferring that exact, functioning code to its own module and passing the reference of its parent form to that module, the program crashes with those (to me), nonsensical errors. It has something to do with relocating the code to it's own module. As typical with Microsoft, somehow the rules change simply by doing so. I'm sure there is a super secret caveat involved with this. Like maybe I need use an Execute command or create an actual Append Query and run it or some such other nonsense. Again, the syntax is correct in the context of being in the OnClick event of a button. But I have two buttons what run 90% of the same code, so I just wanted to mode that common code to it's own module.

  6. #21
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    [QUOTE=Welshgasman;527986]This is your code
    Code:
    When I change the “concatenation” as recommended: … values(" & numApplicantID & "," & frm.txtFldEmployerName & ",… <and so on>…
    Put it all in a string variable, then you can debug.print it.
    Paul would need to be 'Paul' as it is a string. Without those, then Access thinks it is a variable, which it could well be. That is why you get the prompt.
    Strings need to be surrounded by single quotes ' unless it contains a single quote, then triple double quotes works, I think?
    Date literals with # and in mm/dd/yyyy or yyyy-mm-dd format
    Numbers do not need anything

    Also for anything other than one item of criteria, I tend to put the the criteria into a string variable and then debug.print it, until correct then also use that in the code.
    Added benefit is, if you still cannot see the error, you can copy and paste back here the output from the debug.print for someone else to spot it.
    Example:
    tt="Eg'g"
    ? dlookup("FoodID","tblFoods","FoodName = """ & tt & """")

    Look up the syntax when you cannot get it to work.

    https://learn.microsoft.com/en-us/of...oft-access-sql


    the string "Paul" is the actual contents of the text field "txtFldEmployerName" on the form "frm" that was passed to the module. Sorry, I'm not sure what you are trying to say here.

  7. #22
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    the string "Paul" is the actual contents of the text field "txtFldEmployerName" on the form "frm" that was passed to the module. Sorry, I'm not sure what you are trying to say here.
    Because your concatenation is flawed, you are presenting the contents of that control as a variable.
    Access does not know that variable, it does not even know it does not even exist, which it does not, but it cannot find it, so it asks you for the value.

    AGAIN!! put it all into a string variable and Debug.Print that until you get it correct.

    Access is only doing what you are telling it to do/use, whether you regard that as intuitive or not.
    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

  8. #23
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I'm sure there is a super secret caveat involved with this
    I would imagine that would be Scope (and no, not the mouthwash). If you're not aware of it, I suggest you check it out because the problem you describe could certainly be caused by it. It's been a few days since I read what I think is pertinent, and that was about not being able to refer to a control on a form that you've passed to code in a standard module without including its parent (in this case, frm). However you said you tried that but I don't see where you posted that attempt so that anyone could review it. Maybe do that? Or post a zipped db copy and let responders work with what you have. You could remove everything from the copy that is not needed to recreate the issue - just make sure the problem is presented in the copy as well.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #24
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    Quote Originally Posted by Minty View Post
    It might be worth having a quick read here:
    https://medium.com/@NoLongerSet/avoi...s-c036348dd732
    Thanks for the link. I looked at it. It has merit. I will likely comply and start doing things that way. Not sure it will still work passing a form object to a module though. But I have a question. Why would one offer a tool, then offer another tool, suggest the first tool is bad, but don't bother to remove it? If DoCmd.RunSQL is so bad, why even have it? In what circumstances are one command preferred over another? In my case, I live with the errors and warnings in development, but when I submit my final product, I simply turn off those Action Query messages in Access itself so my end users are not bothered by them.

  10. #25
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    My concatenation is not flawed because it works in the context of being in the OnClick Event of a button on the form in question:
    Code:
    Private Sub btnSaveAndExit_Click()    'Make an entry into the Former Employers and Addresses Tables 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);"
        
        DoCmd.RunSQL "Insert into [tblAddresses] ([txtName], [txtAddress1], [txtAddress2], [txtCity], [txtState], [txtZip], [txtCountry], [txtPostCode]) values (txtFldEmployerName, txtFldAddreess1,txtFldAddreess2,txtFldCity,txtFldState,txtFldZipCode,txtFldCountry,txtFldPostalCode);"
        
        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

  11. #26
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by Micron View Post
    I would imagine that would be Scope (and no, not the mouthwash). If you're not aware of it, I suggest you check it out because the problem you describe could certainly be caused by it. It's been a few days since I read what I think is pertinent, and that was about not being able to refer to a control on a form that you've passed to code in a standard module without including its parent (in this case, frm). However you said you tried that but I don't see where you posted that attempt so that anyone could review it. Maybe do that? Or post a zipped db copy and let responders work with what you have. You could remove everything from the copy that is not needed to recreate the issue - just make sure the problem is presented in the copy as well.
    What is strange, is that the form properties are available in the sub when passed as a form object as you would expect, yet Access is not recognising them, which is what the O/P is reporting.
    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

  12. #27
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by pledbetter View Post
    My concatenation is not flawed because it works in the context of being in the OnClick Event of a button on the form in question:
    Code:
    Private Sub btnSaveAndExit_Click()    'Make an entry into the Former Employers and Addresses Tables 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);"
    
    
        
        DoCmd.RunSQL "Insert into [tblAddresses] ([txtName], [txtAddress1], [txtAddress2], [txtCity], [txtState], [txtZip], [txtCountry], [txtPostCode]) values (txtFldEmployerName, txtFldAddreess1,txtFldAddreess2,txtFldCity,txtFldState,txtFldZipCode,txtFldCountry,txtFldPostalCode);"
        
        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
    It is flawed, as what you use in the form is NOTHING like what you are trying to use in the sub
    In the sub, you need to concatenate all the form controls as values, and do it correctly, which you are NOT doing.
    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

  13. #28
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    the form properties are available in the sub when passed as a form object as you would expect, yet Access is not recognising them
    I'd say that's because the value of a control is not a form property - it is a property of the control. I have nothing to test with, which is why I suggested posting a db copy, but I think June7 mentioned this possibility first, somewhere around post 6. Or am I not understanding what you're saying?
    Last edited by Micron; 09-30-2024 at 08:21 AM. Reason: added question
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #29
    Minty is online now VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    Quote Originally Posted by pledbetter View Post
    Thanks for the link. I looked at it. It has merit. I will likely comply and start doing things that way. Not sure it will still work passing a form object to a module though. But I have a question. Why would one offer a tool, then offer another tool, suggest the first tool is bad, but don't bother to remove it? If DoCmd.RunSQL is so bad, why even have it? In what circumstances are one command preferred over another? In my case, I live with the errors and warnings in development, but when I submit my final product, I simply turn off those Action Query messages in Access itself so my end users are not bothered by them.
    There are always multiple ways to skin a cat in Access, some are more suitable than others depending on the task.

    Personally, I always use CurrentDb.Execute , I dislike turning warnings on and off, and prefer my code to simply error whilst developing so I can see where it's gone wrong.
    I also always put the concatenation into a variable, as even thought I've been doing this for years I still get it wrong, or don't pass the value I'm expecting.
    It also allows me to format it in a very readable fashion, so if I need to alter it later it's easy to see where and how.

    E.g. (SQLDate is a function that formats the date correctly as #2024-09-30# and again makes life easier to read.)
    Code:
        Dim strSQL as String
    
        'insert the new record
        strSQL = "INSERT INTO tb_AllocationLists (AllocationList_Author, AllocationList_ModifiedBy, AllocationList_DateModified,AllocationList_DateCreated, AllocationList_Name) "
        strSQL = strSQL & "VALUES (" & iUser & ", " & iUser & ", " & SQLDate(Now()) & ", " & SQLDate(Now()) & ", '" & strListName & "')"
    
        Debug.Print strSQL
    
    
        Set db = CurrentDb
        db.Execute strSQL, dbFailOnError
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  15. #30
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Yes, we need a DB to work with. I am not updating my tables just to work this out.
    Either upload a DB with instructions on how to recreate, or I will just drop out.

    Only needs to have whaever is needed to recreate the issue.

    @Micron I probably used the incorrect word. The control values are present in the Locals window.
    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

Page 2 of 3 FirstFirst 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