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
11 2067 PEB 1,418
Expert 1GB
Hi,
Try:
"INSERT INTO LastGoodData (Date, Symbol, MarketPrice) VALUES (#" & rs1!Date & "#, '" & rs1!Symbol & "', " & str(rs1!MarketPrice) & ")"
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 & ");"
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
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. -
-
-
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")
-
-
Exit_MyProc:
-
rs1.Close
-
rs2.Close
-
Set rs1 = Nothing
-
Set rs2 = Nothing
-
Set db = Nothing
-
-
'Exit Sub
-
-
'Err_MyProc:
-
'Resume Exit_MyProc
-
-
End Sub
-
-
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
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
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.
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?!!!
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. -
-
'On Error GoTo Exit_MyProc
-
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
-
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)
-
-
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
-
DoCmd.RunSQL strINSERT
-
rs1.MoveLast
-
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
-
DoCmd.RunSQL strINSERT
-
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
-
-
Thank you so much for your help...it is finally working....your effort is much appreciated!
Thank you so much for your help...it is finally working....your effort is much appreciated!
You're welcome
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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 (...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |