473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

{SOLVED} INSERT INTO trouble

Does anyone see any problems with my INSERT INTO statement? I know that I have done something wrong, but can't figure it out. Basically, I am trying to create a table called LASTGOODDATA and add records to it with each loop through the program...in a sense writing my debug statement to the table. Any help (and code) would be greatly appreciated!! Thanks!!!


Private Sub cmbFindLastGoodData_Click()
'DoCmd.OpenReport ("rptLASTLSDATA")
Dim db As Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim strSQL As String
Dim strSQLTEMP As String

On Error GoTo Err_MyProc

strSQL = "SELECT DailyPrice.Symbol, DailyPrice.Date, DailyPrice.MarketPrice " & _
"FROM DailyPrice, TempSymbol " & _
"WHERE (((DailyPrice.Symbol) = [TempSymbol]![Symbol])) " & _
"ORDER BY DailyPrice.Symbol, DailyPrice.LocateDate DESC;"

strSQLTEMP = "SELECT Symbol FROM TempSymbol ORDER BY Symbol ASC;"

Set db = CurrentDb
Set rs1 = db.OpenRecordset(strSQL)
Set rs2 = db.OpenRecordset(strSQLTEMP)
'Set rs2 = db.OpenRecordset("TempSymbol")

rs2.MoveFirst
'Do Until rs2.EOF
' Debug.Print rs2!Symbol
' rs2.MoveNext
'Loop
rs1.MoveFirst
Do Until rs2.EOF
Do Until rs1.EOF
If rs2!Symbol = rs1!Symbol Then
If rs1!MarketPrice <> -5.25 Then
Debug.Print rs1!Date, rs1!Symbol, rs1!MarketPrice
INSERT INTO LastGoodData (Date, Symbol, MarketPrice) VALUES (" & rs1!Date & ", " & rs1!Symbol & ", " & rs1!MarketPrice & ")"
'rs1.MoveNext
Exit Do
Else
rs1.MoveNext
End If
Else
rs1.MoveNext
'Debug.Print rs1!Date, rs1!Symbol, rs1!MarketPrice
End If
Loop
rs2.MoveNext
If Not rs2.EOF Then
Debug.Print rs2!Symbol
End If
Loop

rs1.Close
rs2.Close
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing

Exit_MyProc:
rs1.Close
rs2.Close
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing
Exit Sub

Err_MyProc:
Resume Exit_MyProc

End Sub
Nov 10 '06 #1
11 2067
PEB
1,418 Expert 1GB
Hi,

Try:

"INSERT INTO LastGoodData (Date, Symbol, MarketPrice) VALUES (#" & rs1!Date & "#, '" & rs1!Symbol & "', " & str(rs1!MarketPrice) & ")"
Nov 11 '06 #2
MMcCarthy
14,534 Expert Mod 8TB

I am trying to create a table called LASTGOODDATA and add records to it with each loop through the program...in a sense writing my debug statement to the table. Any help (and code) would be greatly appreciated!! Thanks!!!
INSERT INTO statement will only work with an existing table the syntax for make table is different. However, you can't enter one record at a time or it will recreate the table every time.

Instead create the new table first. Add a command to delete all existing records before starting.

DoCmd.RunSQL "DELETE * FROM LastGoodData;"

You can now use your INSERT INTO statement to add new records.
BTW, you need to surround dates with # and text fields with single quotes. I'm assuming here that Symbol is a text field.

INSERT INTO LastGoodData (Date, Symbol, MarketPrice) VALUES (#" & rs1!Date & "#,'" & rs1!Symbol & "'," & rs1!MarketPrice & ");"
Nov 12 '06 #3
thank you very much for the help!! It is working and more importantly I am understanding the code!! I have a code question though...I did get a message that said "object variable or with block variable not set" on the line that says "rs1.close". Have I used these statements correctly? I will include my code again:

Private Sub cmbFindLastGoodData_Click()
'DoCmd.OpenReport ("rptLASTLSDATA")
Dim db As Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim strSQL As String
Dim strSQLTEMP As String
Dim strINSERT As String

On Error GoTo Exit_MyProc

DoCmd.RunSQL "DELETE * FROM LastGoodData;"

strSQL = "SELECT DailyPrice.Symbol, DailyPrice.LocateDate, DailyPrice.MarketPrice " & _
"FROM DailyPrice, TempSymbol " & _
"WHERE (((DailyPrice.Symbol) = [TempSymbol]![Symbol])) " & _
"ORDER BY DailyPrice.Symbol, DailyPrice.LocateDate DESC;"

strSQLTEMP = "SELECT Symbol FROM TempSymbol ORDER BY Symbol ASC;"

Set db = CurrentDb
Set rs1 = db.OpenRecordset(strSQL)
Set rs2 = db.OpenRecordset(strSQLTEMP)
'Set rs2 = db.OpenRecordset("TempSymbol")

rs2.MoveFirst
rs1.MoveFirst
Do Until rs2.EOF
Do Until rs1.EOF
If rs2!Symbol = rs1!Symbol Then
If rs1!MarketPrice <> -5.25 Then
Debug.Print rs1!LocateDate, rs1!Symbol, rs1!MarketPrice
strINSERT = "INSERT INTO LastGoodData (LocateDate, Symbol, MarketPrice) VALUES (#" & rs1!LocateDate & "#, '" & rs1!Symbol & "', " & rs1!MarketPrice & ");"
Debug.Print strINSERT
db.Execute strINSERT, dbFailOnError
'INSERT INTO LastGoodData (LocateDate, Symbol, MarketPrice) VALUES (" & rs1!LocateDate & ", " & rs1!Symbol & ", " & rs1!MarketPrice & ")"
'Print .output; rs1!LocateDate, rs1!Symbol, rs1!MarketPrice;
'rs1.MoveNext
Exit Do
Else
rs1.MoveNext
End If
Else
rs1.MoveNext
End If
Loop
rs2.MoveNext
If Not rs2.EOF Then
Debug.Print rs2!Symbol
End If
Loop
MsgBox ("Table updated successfully")
rs1.Close
rs2.Close
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing

Exit_MyProc:
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing
rs1.Close
rs2.Close

'Exit Sub

'Err_MyProc:
'Resume Exit_MyProc

End Sub

thank you for any additional help!!1
Nov 13 '06 #4
MMcCarthy
14,534 Expert Mod 8TB
thank you very much for the help!! It is working and more importantly I am understanding the code!! I have a code question though...I did get a message that said "object variable or with block variable not set" on the line that says "rs1.close". Have I used these statements correctly? I will include my code again:
You don't need to close the recordsets and database twice.

Exit_MyProc: is not a statement that only happens on Exit it will happen anyway. It is just like a bookmark in the procedure to tell the code where to jump to.

Also you close the recordsets first and set the variables to nothing after.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Private Sub cmbFindLastGoodData_Click()
  4. 'DoCmd.OpenReport ("rptLASTLSDATA")
  5. Dim db As Database
  6. Dim rs1 As DAO.Recordset
  7. Dim rs2 As DAO.Recordset
  8. Dim strSQL As String
  9. Dim strSQLTEMP As String
  10. Dim strINSERT As String
  11.  
  12. On Error GoTo Exit_MyProc
  13.  
  14. DoCmd.RunSQL "DELETE * FROM LastGoodData;"
  15.  
  16. strSQL = "SELECT DailyPrice.Symbol, DailyPrice.LocateDate, DailyPrice.MarketPrice " & _
  17. "FROM DailyPrice, TempSymbol " & _
  18. "WHERE (((DailyPrice.Symbol) = [TempSymbol]![Symbol])) " & _
  19. "ORDER BY DailyPrice.Symbol, DailyPrice.LocateDate DESC;"
  20.  
  21. strSQLTEMP = "SELECT Symbol FROM TempSymbol ORDER BY Symbol ASC;"
  22.  
  23. Set db = CurrentDb
  24. Set rs1 = db.OpenRecordset(strSQL)
  25. Set rs2 = db.OpenRecordset(strSQLTEMP)
  26. 'Set rs2 = db.OpenRecordset("TempSymbol")
  27.  
  28. rs2.MoveFirst
  29. rs1.MoveFirst
  30. Do Until rs2.EOF
  31.   Do Until rs1.EOF
  32.     If rs2!Symbol = rs1!Symbol Then
  33.       If rs1!MarketPrice <> -5.25 Then
  34.         Debug.Print rs1!LocateDate, rs1!Symbol, rs1!MarketPrice
  35.         strINSERT = "INSERT INTO LastGoodData (LocateDate, Symbol, MarketPrice) VALUES (#" & rs1!LocateDate & "#, '" & rs1!Symbol & "', " & rs1!MarketPrice & ");"
  36.         Debug.Print strINSERT
  37.         db.Execute strINSERT, dbFailOnError
  38.         'INSERT INTO LastGoodData (LocateDate, Symbol, MarketPrice) VALUES (" & rs1!LocateDate & ", " & rs1!Symbol & ", " & rs1!MarketPrice & ")"
  39.         'Print .output; rs1!LocateDate, rs1!Symbol, rs1!MarketPrice;
  40.         'rs1.MoveNext
  41.         Exit Do
  42.       Else
  43.         rs1.MoveNext
  44.       End If
  45.     Else
  46.       rs1.MoveNext
  47.     End If
  48.   Loop
  49.     rs2.MoveNext
  50.     If Not rs2.EOF Then
  51.       Debug.Print rs2!Symbol
  52.     End If
  53. Loop
  54.  
  55. MsgBox ("Table updated successfully")
  56.  
  57. Exit_MyProc:
  58. rs1.Close
  59. rs2.Close
  60. Set rs1 = Nothing
  61. Set rs2 = Nothing
  62. Set db = Nothing
  63.  
  64. 'Exit Sub
  65.  
  66. 'Err_MyProc:
  67. 'Resume Exit_MyProc
  68.  
  69. End Sub
  70.  
  71.  
Nov 13 '06 #5
Thank you for the clarification on .close and bookmarks. For some reason...although my new data is being inserted correctly, my msgbox at the bottom of my code is not being executed. Is an error occuring towards the end of my looping to prevent this msgbox from executing? I can't seem to find any problems with the program. Thanks...I have again included NEW version of code.

Dim db As Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim strSQL As String
Dim strSQLTEMP As String
Dim strINSERT As String

On Error GoTo Exit_MyProc

DoCmd.RunSQL "DELETE * FROM LastGoodData;"

strSQL = "SELECT DailyPrice.Symbol, DailyPrice.LocateDate, DailyPrice.MarketPrice " & _
"FROM DailyPrice, TempSymbol " & _
"WHERE (((DailyPrice.Symbol) = [TempSymbol]![Symbol])) " & _
"ORDER BY DailyPrice.Symbol, DailyPrice.LocateDate DESC;"

strSQLTEMP = "SELECT Symbol FROM TempSymbol ORDER BY Symbol ASC;"

Set db = CurrentDb
Set rs1 = db.OpenRecordset(strSQL)
Set rs2 = db.OpenRecordset(strSQLTEMP)
'Set rs2 = db.OpenRecordset("TempSymbol")

rs2.MoveFirst
rs1.MoveFirst
Do Until rs2.EOF
Do Until rs1.EOF
If rs2!Symbol = rs1!Symbol Then
If rs1!MarketPrice <> -5.25 Then
Debug.Print rs1!LocateDate, rs1!Symbol, rs1!MarketPrice
strINSERT = "INSERT INTO LastGoodData (LocateDate, Symbol, MarketPrice) VALUES (#" & rs1!LocateDate & "#, '" & rs1!Symbol & "', " & rs1!MarketPrice & ");"
Debug.Print strINSERT
db.Execute strINSERT, dbFailOnError
Exit Do
Else 'only for -5.25 data
rs1.MoveNext
If rs1.EOF Then
strINSERT = "INSERT INTO LastGoodData (LocateDate, Symbol, MarketPrice) VALUES (#" & rs1!LocateDate & "#, '" & rs1!Symbol & "', " & rs1!MarketPrice & ");"
Debug.Print strINSERT
db.Execute strINSERT, dbFailOnError
Exit Do
End If
End If
Else
rs1.MoveNext
End If
Loop
rs2.MoveNext
If Not rs2.EOF Then
Debug.Print rs2!Symbol
End If
Loop
MsgBox ("Table updated successfully")

Exit_MyProc:
rs1.Close
rs2.Close
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing

End Sub
Nov 13 '06 #6
MMcCarthy
14,534 Expert Mod 8TB
Take the brackets away. Brackets on any function implies that a return value is expected. In this case the default is vbOK. It should have given an error.

MsgBox "Table updated successfully"
Mary

Thank you for the clarification on .close and bookmarks. For some reason...although my new data is being inserted correctly, my msgbox at the bottom of my code is not being executed. Is an error occuring towards the end of my looping to prevent this msgbox from executing? I can't seem to find any problems with the program. Thanks...I have again included NEW version of code.

Dim db As Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim strSQL As String
Dim strSQLTEMP As String
Dim strINSERT As String

On Error GoTo Exit_MyProc

DoCmd.RunSQL "DELETE * FROM LastGoodData;"

strSQL = "SELECT DailyPrice.Symbol, DailyPrice.LocateDate, DailyPrice.MarketPrice " & _
"FROM DailyPrice, TempSymbol " & _
"WHERE (((DailyPrice.Symbol) = [TempSymbol]![Symbol])) " & _
"ORDER BY DailyPrice.Symbol, DailyPrice.LocateDate DESC;"

strSQLTEMP = "SELECT Symbol FROM TempSymbol ORDER BY Symbol ASC;"

Set db = CurrentDb
Set rs1 = db.OpenRecordset(strSQL)
Set rs2 = db.OpenRecordset(strSQLTEMP)
'Set rs2 = db.OpenRecordset("TempSymbol")

rs2.MoveFirst
rs1.MoveFirst
Do Until rs2.EOF
Do Until rs1.EOF
If rs2!Symbol = rs1!Symbol Then
If rs1!MarketPrice <> -5.25 Then
Debug.Print rs1!LocateDate, rs1!Symbol, rs1!MarketPrice
strINSERT = "INSERT INTO LastGoodData (LocateDate, Symbol, MarketPrice) VALUES (#" & rs1!LocateDate & "#, '" & rs1!Symbol & "', " & rs1!MarketPrice & ");"
Debug.Print strINSERT
db.Execute strINSERT, dbFailOnError
Exit Do
Else 'only for -5.25 data
rs1.MoveNext
If rs1.EOF Then
strINSERT = "INSERT INTO LastGoodData (LocateDate, Symbol, MarketPrice) VALUES (#" & rs1!LocateDate & "#, '" & rs1!Symbol & "', " & rs1!MarketPrice & ");"
Debug.Print strINSERT
db.Execute strINSERT, dbFailOnError
Exit Do
End If
End If
Else
rs1.MoveNext
End If
Loop
rs2.MoveNext
If Not rs2.EOF Then
Debug.Print rs2!Symbol
End If
Loop
MsgBox ("Table updated successfully")

Exit_MyProc:
rs1.Close
rs2.Close
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing

End Sub
Nov 13 '06 #7
NeoPa
32,556 Expert Mod 16PB
Or use 'Call'.
I like Call as it enables me to use subroutines OR functions without worrying about the return value - when that's what I need.
Nov 13 '06 #8
I have removed the parenthesis for the msgbox. It still does not display. I have 5 symbols that I loop through. The last symbol is a value of -5.25. I believe there is an error occuring while processing this symbols value that bounces me out to the error trapping and doesn't allow me to display the msgbox. could it be in my trying to db.execute the second time...

[code]
If rs1.EOF Then
MsgBox "rs1 eof"
strINSERT = "INSERT INTO LastGoodData (LocateDate, Symbol, MarketPrice) VALUES (#" & rs1!LocateDate & "#, '" & rs1!Symbol & "', " & rs1!MarketPrice & ");"
Debug.Print strINSERT
db.Execute strINSERT, dbFailOnError
Exit Do
..... rest of code...

The msgbox "rs1 eof" DOES display! Any help?!!!
Nov 13 '06 #9
MMcCarthy
14,534 Expert Mod 8TB
I've changed some of the code and commented out the On Error GoTo line.

This means that the code should stop running and go into debug mode when it hits the error. Click on the debug button and it should highlight the problem line in yellow which will tell us where to look.

Expand|Select|Wrap|Line Numbers
  1.  
  2. 'On Error GoTo Exit_MyProc
  3. Dim db As Database
  4. Dim rs1 As DAO.Recordset
  5. Dim rs2 As DAO.Recordset
  6. Dim strSQL As String
  7. Dim strSQLTEMP As String
  8. Dim strINSERT As String
  9.     DoCmd.RunSQL "DELETE * FROM LastGoodData;"
  10.  
  11.     strSQL = "SELECT DailyPrice.Symbol, DailyPrice.LocateDate, DailyPrice.MarketPrice " & _
  12.     "FROM DailyPrice, TempSymbol " & _
  13.     "WHERE (((DailyPrice.Symbol) = [TempSymbol]![Symbol])) " & _
  14.     "ORDER BY DailyPrice.Symbol, DailyPrice.LocateDate DESC;"
  15.  
  16.     strSQLTEMP = "SELECT Symbol FROM TempSymbol ORDER BY Symbol ASC;"
  17.  
  18.     Set db = CurrentDb
  19.     Set rs1 = db.OpenRecordset(strSQL)
  20.     Set rs2 = db.OpenRecordset(strSQLTEMP)
  21.  
  22.     rs2.MoveFirst
  23.     rs1.MoveFirst
  24.     Do Until rs2.EOF
  25.         Do Until rs1.EOF
  26.             If rs2!Symbol = rs1!Symbol Then
  27.                 If rs1!MarketPrice <> -5.25 Then
  28.                     Debug.Print rs1!LocateDate, rs1!Symbol, rs1!MarketPrice
  29.                     strINSERT = "INSERT INTO LastGoodData (LocateDate, Symbol, MarketPrice) VALUES (#" & rs1!LocateDate & "#, '" & rs1!Symbol & "', " & rs1!MarketPrice & ");"
  30.                     Debug.Print strINSERT
  31.                     DoCmd.RunSQL strINSERT
  32.                     rs1.MoveLast
  33.                 Else 'only for -5.25 data
  34.                     rs1.MoveNext
  35.                     If rs1.EOF Then
  36.                         strINSERT = "INSERT INTO LastGoodData (LocateDate, Symbol, MarketPrice) VALUES (#" & rs1!LocateDate & "#, '" & rs1!Symbol & "', " & rs1!MarketPrice & ");"
  37.                         Debug.Print strINSERT
  38.                         DoCmd.RunSQL strINSERT
  39.                     End If
  40.                 End If
  41.             Else
  42.                 rs1.MoveNext
  43.             End If
  44.         Loop
  45.         rs2.MoveNext
  46.         If Not rs2.EOF Then
  47.         Debug.Print rs2!Symbol
  48.         End If
  49.     Loop
  50.     MsgBox "Table updated successfully"
  51.  
  52. Exit_MyProc:
  53.     rs1.Close
  54.     rs2.Close
  55.     Set rs1 = Nothing
  56.     Set rs2 = Nothing
  57.     Set db = Nothing
  58. End Sub
  59.  
  60.  
Nov 13 '06 #10
Thank you so much for your help...it is finally working....your effort is much appreciated!
Nov 14 '06 #11
MMcCarthy
14,534 Expert Mod 8TB
Thank you so much for your help...it is finally working....your effort is much appreciated!
You're welcome
Nov 14 '06 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: DTB | last post by:
I am having trouble creating an INSTEAD OF trigger in SQL Server to replicate a BEFORE UPDATE trigger from ORACLE. Here is a sample of the ORACLE BEFORE UPDATE trigger: CREATE TRIGGER myTRIGGER ON...
3
by: Thomas R. Hummel | last post by:
Hi, As I wrote my message the solution came to me, so I thought I would post anyway for others to see in case it was useful: Here is some sample DDL for this question: CREATE TABLE Source (...
2
by: User | last post by:
Hi ! I have a sql insert function with a buttonin my page, and when i click on refresh button of IE. When i click my button, i have a new records but after when i click on the refresh button of IE...
7
by: Gary Paris | last post by:
I have a bound datagrid on my windowsform. I have the following declarations in the global module of the project: Public eDS As DataSet Public eDA As OleDb.OleDbDataAdapter In the Load...
7
by: David Bear | last post by:
I have a dictionary that contains a row of data intended for a data base. The dictionary keys are the field names. The values are the values to be inserted. I am looking for a good pythonic...
3
by: Jacob Lyles | last post by:
Howdy, I'm a but of a newbie and I'd appreciate some help with a MySQL issue I'm having. I'm trying to insert some data into MySQL from a POST form but the query breaks whenever a user fails to...
3
by: rhaazy | last post by:
Using ms sql 2000 I have 2 tables. I have a table which has information regarding a computer scan. Each record in this table has a column called MAC which is the unique ID for each Scan. The...
5
by: mabond | last post by:
Hi VB.NET 2005 Express edition Microsoft Access 2000 (SP-3) Having trouble writing an "insert into" command for a Microsoft table I'm accessing through oledb. I've tried to follow the same...
1
by: Wes Groleau | last post by:
INSERT INTO X ...... ( A, B, C ) INSERT INTO Y ...... ( J, K, L ) If Y has a foreign key M which is the primary key D of X, is there an easy and/or efficient way to have SQL Server assign D,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.