Results 1 to 15 of 15
  1. #1
    chromachem is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    49

    Use value from form in query

    We have the following query


    Code:
     
    INSERT INTO tblTempSELECT *
    FROM (SELECT Replace(f4,'.d','') AS LabID, F6 AS [Level], f7 AS [Date], F8 AS PFBS, F11 AS [Symmetry PFBS], F14 AS [13C6-PFHxA], F20 AS PFHxA, F23 AS [Symmetry PFHxS], F26 AS [13C3-HFPO-DA], F32 AS [HFPO-DA], F38 AS PFHpA, F44 AS PFHxS, F50 AS ADONA, F56 AS PFOA, F62 AS PFNA, F68 AS PFOS, F74 AS [9Cl-PF3ONS], F80 AS [13C9-PFDA], F86 AS PFDA, F92 AS NMeFOSAA, F98 AS PFUnA, F104 AS [d5-NEtFOSAA], F110 AS NEtFOSAA, F116 AS [11Cl-PF3OUdS], F122 AS PFDoA, F128 AS PFTrDA, F134 AS PFTA FROM [TEXT;DATABASE=F:\PFAS Exported Data\;HDR=No].072823.csv WHERE F4 Not Like 'Blank*' And F4<>'Data File')  AS txt;
    We have a form with a command button named cmdImport.



    Is there a way to get the value from the form into the query to replace "072823" ?

    If we want to import data from today we would enter 012224 in the form. the data file is 012224.csv.

    Thanks

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2019
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Can you post a copy of the db
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Concatenation?
    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. #4
    chromachem is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    49
    Pardon me for my lack of DB programming knowledge. My experience is excel VBA. Just learning SQL.

    I am very familiar with concatenating in excel VBA. I don't have a clue how to concatenate an SQL statement using the value from a text box in a form.
    I tried to find an example online to no avail. Most likely I didn't ask the question online correctly.

    If anyone could give an example of how to do this type of concatenation of the value from a text box in a form, that would be great. I also see where I wasn't clear in my original post. The value is in the field called txtSequence. The query in my original posting is executed by clicking on the cmdImport command button. My aplologies for not being clearer.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Try
    Code:
    Dim strSQL As String
    
    strSQL = "INSERT INTO tblTempSELECT *
    FROM (SELECT Replace(f4,'.d','') AS LabID, F6 AS [Level], f7 AS [Date], F8 AS PFBS, F11 AS [Symmetry PFBS], F14 AS [13C6-PFHxA], F20 AS PFHxA, F23 AS [Symmetry PFHxS], F26 AS [13C3-HFPO-DA], F32 AS [HFPO-DA], F38 AS PFHpA, F44 AS PFHxS, F50 AS ADONA, F56 AS PFOA, F62 AS PFNA, F68 AS PFOS, F74 AS [9Cl-PF3ONS], F80 AS [13C9-PFDA], F86 AS PFDA, F92 AS NMeFOSAA, F98 AS PFUnA, F104 AS [d5-NEtFOSAA], F110 AS NEtFOSAA, F116 AS [11Cl-PF3OUdS], F122 AS PFDoA, F128 AS PFTrDA, F134 AS PFTA FROM [TEXT;DATABASE=F:\PFAS Exported Data\;HDR=No]." & Forms!YourForm!YourControl & ".csv WHERE F4 Not Like 'Blank*' And F4<>'Data File')  AS txt";
    
    Debug.Print strSQL
    That way you can always check your syntax and when correct use that in the Execute command.

    If the code is in your actual form you can just use Me.YourControl where that is the name of your control with your 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

  6. #6
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I am very familiar with concatenating in excel VBA. I don't have a clue how to concatenate an SQL statement using the value from a text box in a form.
    The rules are the same. The tricky point will be using the correct control reference. As WGM suggests, if the code is in the form you can use something like
    Code:
    " ... f7 AS [Date], " & Me.ControlNameHere & " AS PFBS, F11 AS [Symmetry PFBS], F14 AS [13C6-PFHxA],..."
    The suggestion to debug.print the sql is a good one since you will need to ensure that strings are delimited (' or "") as well as dates (#) but no delimiting for values that are of type Number (not the same as Numeric). Other points regarding getting a reference to the form control:
    - the form has to be open or
    - you have to pass values to another form/report when it opens
    - if the control is a combo, the returned value is often the record id and is not what you need
    - it is a bit tricky to refer to combo columns in a query

    EDIT - date is a poor choice for a field name.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    chromachem is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    49
    OK. Getting there I think.

    The form name is frmImport537Data
    The unbound text box is txtSequence. Today the value we would enter is 012324. To me this is a numeric value - we need the leading Zero.
    There is a command box on the form (cmdImport) that we click after entering a value used to create the name for the import file.

    We have built an event for the cmdImport command button as follows:

    Code:
    Private Sub cmdImport_Click()
    
    Dim strSQL As String
    
    
    
    
    strSQL = "INSERT INTO tblTempSELECT * FROM (SELECT Replace(f4,'.d','') AS LabID, F6 AS [Level], f7 AS [Date], F8 AS PFBS, F11 AS [Symmetry PFBS], F14 AS [13C6-PFHxA], F20 AS PFHxA, F23 AS [Symmetry PFHxS], F26 AS [13C3-HFPO-DA], F32 AS [HFPO-DA], F38 AS PFHpA, F44 AS PFHxS, F50 AS ADONA, F56 AS PFOA, F62 AS PFNA, F68 AS PFOS, F74 AS [9Cl-PF3ONS], F80 AS [13C9-PFDA], F86 AS PFDA, F92 AS NMeFOSAA, F98 AS PFUnA, F104 AS [d5-NEtFOSAA], F110 AS NEtFOSAA, F116 AS [11Cl-PF3OUdS], F122 AS PFDoA, F128 AS PFTrDA, F134 AS PFTA FROM [TEXT;DATABASE=F:\PFAS Exported Data\;HDR=No]." & Forms!frmImport537Data!txtSequence & ".csv WHERE F4 Not Like 'Blank*' And F4<>'Data File')  AS txt"
    
    
    Debug.Print strSQL
    
    
    'DoCmd.OpenQuery "qryStructure"
    DoCmd.OpenQuery "qryAppend"
    DoCmd.OpenQuery "qry537Upsert"
       
    
    
    End Sub

    Is there a problem with the fact that txtSequence contains a numeric value? I'm guessing I have the correct string stored in strSQL. Our entire lab operation is built around using the mmddyy format to name sequences. Its way too late to change that.

    Query qryAppend originally contained the sql in the original post.

    Now that strSQL hopefully contains the string I need, what do I put in qryAppend? Do I somehow run the query where I performed the concatenation (cmdImport_Click() )?

    As always, any help is greatly appreciated.

  8. #8
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Does your .Print look right? Did you copy and paste into a new query and try to switch to Datasheet view? What happened/happens?
    Also,
    - an unbound textbox may not have a value. Can happen if it has something in it but focus never leaves the control. You may have to use its .Text property instead of the default .Value property.
    - add Debug.Print TypeName(Forms!frmImport537Data!txtSequence.Text) and
    - add Debug.Print TypeName(Forms!frmImport537Data!txtSequence.Value)
    and check the outputs. 1234 is numeric but it can be number or text data type. If it's text, you need to add text delimiters to your concatenation. If you get no printout for .Value then it proves my point re unbound textbox, in which case you'd be passing Null or zls to the query sql.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by chromachem View Post
    Now that strSQL hopefully contains the string I need, what do I put in qryAppend? Do I somehow run the query where I performed the concatenation (cmdImport_Click() )?
    A couple of ways, most commonly

    CurrentDb.Execute strSQL, dbFailOnError

    That would be in place of the OpenQuery line(s).

    You wouldn't want delimiters around the value as it's being used for a file name, not data. Focus shouldn't be an issue here; since the code is behind a button focus will have left the textbox.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Today the value we would enter is 012324. To me this is a numeric value - we need the leading Zero
    numbers do not have leading zeros, if they do, then they have to be text.

    You can use the format property to display leading zeros (simply "000000") for numbers, but the underlying value remains numeric. This is not an access issue, it is the way it is in the world. Open excel and type 012324 in a cell, see what happens - the leading zero is stripped off. Still in excel put a single quote before entering your number i.e. '012324. That retains the leading zero but with a warning 'number stored as text'.

  11. #11
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    numbers do not have leading zeros, if they do, then they have to be text.
    Agreed, hence my suggestions. People often don't know the difference between data types where numeric characters are concerned. I was hoping to dispel any doubt about what is posted and what the source is since they are not always the same despite what is claimed.
    Last edited by Micron; 01-23-2024 at 01:18 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  12. #12
    chromachem is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    49
    Code:
    strSQL = "INSERT INTO tblTemp SELECT * FROM (SELECT Replace(f4,'.d','') AS LabID, F6 AS [Level], f7 AS [Date], F8 AS PFBS, F11 AS [Symmetry PFBS], F14 AS [13C6-PFHxA], F20 AS PFHxA, F23 AS [Symmetry PFHxS], F26 AS [13C3-HFPO-DA], F32 AS [HFPO-DA], F38 AS PFHpA, F44 AS PFHxS, F50 AS ADONA, F56 AS PFOA, F62 AS PFNA, F68 AS PFOS, F74 AS [9Cl-PF3ONS], F80 AS [13C9-PFDA], F86 AS PFDA, F92 AS NMeFOSAA, F98 AS PFUnA, F104 AS [d5-NEtFOSAA], F110 AS NEtFOSAA, F116 AS [11Cl-PF3OUdS], F122 AS PFDoA, F128 AS PFTrDA, F134 AS PFTA FROM [TEXT;DATABASE=F:\PFAS Exported Data\;HDR=No]." & Forms!frmImport537Data!txtSequence & ".csv WHERE F4 Not Like 'Blank*' And F4<>'Data File')  AS txt;"

  13. #13
    chromachem is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    49
    Got too excited a moment ago and posted without context.

    THANK YOU to all who helped!!!! So much appreciated. I had to move the semi colon at the end of the statement and we are good to go!!! Here is the final strSQL concatenation
    Code:
    strSQL = "INSERT INTO tblTemp SELECT * FROM (SELECT Replace(f4,'.d','') AS LabID, F6 AS [Level], f7 AS [Date], F8 AS PFBS, F11 AS [Symmetry PFBS], F14 AS [13C6-PFHxA], F20 AS PFHxA, F23 AS [Symmetry PFHxS], F26 AS [13C3-HFPO-DA], F32 AS [HFPO-DA], F38 AS PFHpA, F44 AS PFHxS, F50 AS ADONA, F56 AS PFOA, F62 AS PFNA, F68 AS PFOS, F74 AS [9Cl-PF3ONS], F80 AS [13C9-PFDA], F86 AS PFDA, F92 AS NMeFOSAA, F98 AS PFUnA, F104 AS [d5-NEtFOSAA], F110 AS NEtFOSAA, F116 AS [11Cl-PF3OUdS], F122 AS PFDoA, F128 AS PFTrDA, F134 AS PFTA FROM [TEXT;DATABASE=F:\PFAS Exported Data\;HDR=No]." & Forms!frmImport537Data!txtSequence & ".csv WHERE F4 Not Like 'Blank*' And F4<>'Data File')  AS txt;"
    Then used Baldy's code to execute from within the build event of the text box in the form.

    Code:
    CurrentDb.Execute strSQL, dbFailOnError
    Slick as can be!!!!!!

    Thanks again to all.

  14. #14
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    FYI, pretty sure that if you run a sql statement from vba, you don't have to use the trailing ; .
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  15. #15
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    I never use them.
    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

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

Similar Threads

  1. Replies: 3
    Last Post: 07-12-2017, 12:03 PM
  2. Replies: 6
    Last Post: 08-05-2016, 10:57 PM
  3. Replies: 0
    Last Post: 06-03-2014, 05:15 AM
  4. Replies: 7
    Last Post: 06-10-2013, 12:40 PM
  5. Replies: 7
    Last Post: 05-02-2012, 08:04 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