Loop through a table to find value and us in SQL query (1 Viewer)

Cerodalo

New member
Local time
Today, 04:53
Joined
Sep 23, 2019
Messages
4
So after years of getting great value from the site I've been stumped and need to post my first question! Thanks for all the advice in advance and sorry of the questions seems overly simple.

I'm trying to grab a name in the ProductsXref table and use it in a query that appends records to a table. I tried to use this code below but it doesn't work and I get an error. I'm struggling to know if I'm stepping through the rows AND if the SQL code will work. Thanks!


Dim rs As DAO.Recordset
Dim sSQL As String
Dim aSQL As String
Dim sValue As String

sSQL = "SELECT * FROM ProductsXref"
Set rs = CurrentDb.OpenRecordset(sSQL)

DoCmd.SetWarnings False
rs.MoveFirst

If Not rs.BOF And Not rs.EOF Then
rs.MoveFirst
While (Not rs.EOF)
sValue = rs("Myfield")
'sSQL = "SELECT V2.customer_id, V2.customer_name, V2." & sValue & ", svalue AS Expr1 FROM V2 WHERE (((V2." & sValue & ")>0));"
DoCmd.RunSQL "INSERT INTO V2_Amend ( customer_id, customer_name, svalue, Expr1 )SELECT V2.customer_id, V2.customer_name, V2." & sValue & ", svalue AS Expr1 FROM V2 WHERE (((V2." & sValue & ")>0));"
rs.MoveNext
Wend
End If

rs.Close
Set rs = Nothing
End Sub
 

moke123

AWF VIP
Local time
Yesterday, 21:53
Joined
Jan 11, 2013
Messages
3,913
Add some debug.prints to the Sql strings before you execute them so you can see what they resolve to.

what is the specific error ?

the parts in red look problematic to me.

DoCmd.RunSQL "INSERT INTO V2_Amend ( customer_id, customer_name, svalue, Expr1 )SELECT V2.customer_id, V2.customer_name, V2." & sValue & ", svalue AS Expr1 FROM V2 WHERE (((V2." & sValue & ")>0));"
 
Last edited:

Mark_

Longboard on the internet
Local time
Yesterday, 18:53
Joined
Sep 12, 2017
Messages
2,111
As important, what is the advantage to you of having duplicate data? Is the 2nd table not one that can be related to the original?
 

June7

AWF VIP
Local time
Yesterday, 17:53
Joined
Mar 9, 2014
Messages
5,470
Please post code between CODE tags to retain indentation and readability.

What is error message? Which line triggers error?

Consider:
Code:
While (Not rs.EOF)
    sValue = rs("Myfield")
    DoCmd.RunSQL "INSERT INTO V2_Amend ( customer_id, customer_name, " & sValue & ") " & _
           "SELECT customer_id, customer_name, " & sValue & " FROM V2 WHERE " & sValue & ">0;"
    rs.MoveNext
Wend

If field names have spaces or punctuation/special characters, need to use [] delimiters.
 
Last edited:

moke123

AWF VIP
Local time
Yesterday, 21:53
Joined
Jan 11, 2013
Messages
3,913
Another thing I just noticed is you are setting warnings to false and not re-setting them back to true. You should use the .Execute method to avoid having to do this.

Pretty safe to say that your Sql strings are not resolving to what you think they are.
To test your Sql you would do something like
Code:
strSql = "Select * from someTable where SomeField = " & SomeVariable
Debug.Print strSql
currentDB.Execute strSql,dbfailOnError

It would be helpful if you showed us your table structures and explain what you want to copy between tables.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:53
Joined
May 7, 2009
Messages
19,230
the question is, what is sValue?
you declare it as string.
Code:
Dim sValue As String
then put some value on it. for you know, you can get Null value:
Code:
sValue = rs("Myfield")
does it contain a "fieldname" in it?
maybe, something like:
Code:
DoCmd.RunSQL "INSERT INTO V2_Amend (customer_id, customer_name, " & 
     svalue & ") SELECT V2.customer_id, V2.customer_name, V2." & sValue & " FROM V2 WHERE (((V2." & sValue & ")>0));"
 

Cerodalo

New member
Local time
Today, 04:53
Joined
Sep 23, 2019
Messages
4
That is a big part of my issue I think. I don't know the correct format for the field references. Also, I want the field reference to also be a label in the query, which is usually set with "label" inverted commas.

Add some debug.prints to the Sql strings before you execute them so you can see what they resolve to.

what is the specific error ?

the parts in red look problematic to me.

DoCmd.RunSQL "INSERT INTO V2_Amend ( customer_id, customer_name, svalue, Expr1 )SELECT V2.customer_id, V2.customer_name, V2." & sValue & ", svalue AS Expr1 FROM V2 WHERE (((V2." & sValue & ")>0));"
 

Cerodalo

New member
Local time
Today, 04:53
Joined
Sep 23, 2019
Messages
4
I think I know my problem/limitation.

I'm trying to use a record value from a table as a reference to a column name in a query (see bold below). I don't know if this is possible and if so how to make it work.

INSERT INTO V2_Amend ( customer_id, customer_name, Defendant_Watch_46 )
SELECT V2.customer_id, V2.customer_name, V2.Defendant_Watch_46
FROM V2
WHERE (((V2.Defendant_Watch_46)>0));

I'm trying to get the record value from the following code;

DoCmd.SetWarnings False
rs.MoveFirst
If Not rs.BOF And Not rs.EOF Then
rs.MoveFirst
While (Not rs.EOF)
Let sValue = rs("Myfield") <--this is the record value
DoCmd.RunSQL "QUERY HERE" <-- SQL above to be used here
rs.MoveNext
Wend
End If
rs.Close
Set rs = Nothing
 

June7

AWF VIP
Local time
Yesterday, 17:53
Joined
Mar 9, 2014
Messages
5,470
Did you see post #5?

A field name like Defendant_Watch_46 makes me suspect this is not a normalized data structure. Are there multiple similar name fields: Defendant_Watch_1, Defendant_Watch_2, etc.?

Seems there will be a lot of empty fields with this structure as code appears to be creating new records where only 3 fields out of how many receive data and one of those fields changes with each iteration.

Suggest you provide db for analysis. Follow instructions at bottom of my post.
 
Last edited:

Cerodalo

New member
Local time
Today, 04:53
Joined
Sep 23, 2019
Messages
4
Yes, thanks, did see reply 5.

Yes, there are a lot of columns with similar naming conventions. It comes from a file pulled from a billing system that is provided in the format.

Once you get the DB you'll see the V2 table which is the main table. In essence I'm trying to figure out which customers are running which products, which are provided as column names and values in the subsequent fields.
 

Attachments

  • Database2 (2).zip
    83.5 KB · Views: 484

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:53
Joined
May 7, 2009
Messages
19,230
You have serious table normalization problem.
here test this.
 

Attachments

  • Database2 (2).zip
    82.6 KB · Views: 301

Mark_

Longboard on the internet
Local time
Yesterday, 18:53
Joined
Sep 12, 2017
Messages
2,111
To help us aid you better, is your intent to import the excel sheets into temporary tables then move them to a normalized set of tables? If yes, is this a one time import or will you need to do it again?
 

Users who are viewing this thread

Top Bottom