本文介绍了{求助}插入麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 有没有人看到我的INSERT INTO语句有任何问题?我知道我做错了什么,但不能弄清楚。基本上,我正在尝试创建一个名为LASTGOODDATA的表,并通过程序中的每个循环向其添加记录......在某种意义上,我将调试语句写入表中。任何帮助(和代码)将不胜感激!谢谢!!! Private Sub cmbFindLastGoodData_Click() ''DoCmd.OpenReport(" rptLASTLSDATA") Dim db作为数据库 Dim rs1作为DAO.Recordset Dim rs2作为DAO.Recordset Dim strSQL As String Dim strSQLTEMP As String 错误GoTo Err_MyProc strSQL =" SELECT DailyPrice.Symbol,DailyPrice.Date,DailyPrice .MarketPrice &安培; _ " FROM DailyPrice,TempSymbol" &安培; _ " WHERE(((DailyPrice.Symbol)= [TempSymbol]![符号])) &安培; _ " ORDER BY DailyPrice.Symbol,DailyPrice.LocateDate DESC;" strSQLTEMP =" SELECT Symbol FROM TempSymbol ORDER BY Symbol ASC;" ; 设置db = CurrentDb 设置rs1 = db.OpenRecordset(strSQL) 设置rs2 = db.OpenRecordset(strSQLTEMP) ) ''设置rs2 = db.OpenRecordset(" TempSymbol") rs2.MoveFirst ''Do Until rs2.EOF ''Debug.Print rs2!符号 ''rs2.MoveNext ''循环 rs1.MoveFirst 直到rs2.EOF 直到rs1.EOF 如果rs2!符号= rs1!符号那么 如果rs1!MarketPrice<> -5.25然后 Debug.Print rs1!Date,rs1!Symbol,rs1!MarketPrice INSERT INTO LastGoodData(Date,Symbol,MarketPrice)VALUES(& rs1) !Date&","& rs1!Symbol&","& rs1!MarketPrice&")" ''rs1.MoveNext 退出运行 否则 rs1.MoveNext 结束如果 否则 rs1.MoveNext ''Debug.Print rs1!日期,rs1!符号,rs1!MarketPrice 结束如果 循环 rs2.MoveNext 如果不是rs2.EOF那么 Debug.Print rs2!符号 结束如果 循环 rs1.Close rs2.Close 设置rs1 =无任何 设置rs2 =没什么 设置db = Nothing Exit_MyPro c: rs1.Close rs2.Close 设置rs1 =无什么 设置rs2 =无/> 设置db = Nothing 退出Sub Err_MyProc: 恢复Exit_MyProc 结束Sub 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推荐答案 INSERT INTO语句仅适用于现有表,make表的语法不同。但是,您不能一次输入一条记录,或者每次都会重新创建表格。 而是首先创建新表格。添加命令以在开始之前删除所有现有记录。 DoCmd.RunSQLDELETE * FROM LastGoodData; 您现在可以使用INSERT INTO语句添加新记录。 顺便说一句,你需要用#和文本字段用单引号括起日期。我在这里假设Symbol是一个文本字段。 INSERT INTO LastGoodData(Date,Symbol,MarketPrice)VALUES(#"& rs1!Date&"# ,''"& rs1!符号&"'',& rs1!MarketPrice&");" 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 & ");" 这篇关于{求助}插入麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-22 13:38