如何在MS Access中使用VBA将值插入数据库表 [英] How to insert values into the database table using VBA in MS access

查看:211
本文介绍了如何在MS Access中使用VBA将值插入数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用访问权限.我试图在数据库中插入几行;但是,由于发生错误,我被卡住了:

I've started to use access recently. I am trying to insert a few rows into the database; however, I am stuck as it is throwing an error:

参数太少.

Too few parameters.

我有一个表测试,其中只有一列名为start_date我想在两个日期之间插入所有日期,例如,如果我考虑将2014年7月7日至2014年7月7日,我需要将日期设为1/7/2014,2/7/2014,3/7/2014在我的表格中,但是我在插入所使用的代码时遇到问题

I have a table test with only one column in it named start_date I want to insert all the dates between two dates for example if I consider 1/7/2014 to 3/7/2014 I need dates as 1/7/2014,2/7/2014,3/7/2014 in my table, but I have problem inserting the code I used is as follows

Private Sub createRec_Click()
Dim StrSQL As String
Dim InDate As Date
Dim DatDiff As Integer
Dim db As database
InDate=Me.FromDateTxt
'here I have used a code to find out the difference between two dates that i've not written
For i = 1 To DatDiff
StrSQL = "INSERT INTO Test (Start_Date) VALUES ('" & InDate & "' );"
StrSQL = StrSQL & "SELECT 'Test'"
db.Execute StrSQL
db.close
i=i+1
next i
End Sub

我的代码在Db.Execuite StrSQL行中引发错误 由于参数太少. 希望有人可以帮助我解决这个问题. 提前谢谢

My code throws an error in the line Db.Execuite StrSQL as too few parameters. Hope somebody could help me with this issue. Thanks In advance

推荐答案

由于您提到您是一个刚接触的人,所以我不得不邀请您先删除代码中的错误(不完整的for循环和SQL语句) .否则,您肯定需要for循环才能在某个范围内插入日期.

since you mentioned you are quite new to access, i had to invite you to first remove the errors in the code (the incomplete for loop and the SQL statement). Otherwise, you surely need the for loop to insert dates in a certain range.

现在,请使用下面的代码将日期值插入表格中.我已经测试了代码,并且可以正常工作.您也可以尝试.之后,添加适合您情况的for循环

Now, please use the code below to insert the date values into your table. I have tested the code and it works. You can try it too. After that, add your for loop to suit your scenario

Dim StrSQL As String
Dim InDate As Date
Dim DatDiff As Integer

InDate = Me.FromDateTxt

StrSQL = "INSERT INTO Test (Start_Date) VALUES ('" & InDate & "' );"

DoCmd.SetWarnings False
DoCmd.RunSQL StrSQL
DoCmd.SetWarnings True

这篇关于如何在MS Access中使用VBA将值插入数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆