使用VBA插入查询 [英] Insert Into query using VBA

查看:109
本文介绍了使用VBA插入查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我为什么INSERT INTO会产生语法错误


Private Sub Command7_Click()

Dim strUSER As String

strUSER = Nz(DLookup([user],table2,[user] =''"& username.Value&"''")," nouser")

如果strUSER =" nouser"然后

INSERT INTO table2;

VALUES(username.Value,password.Value);

MsgBox" User added"

Else

MsgBox" user already exists"

Can someone please tell me why INSERT INTO generates syntax error

Private Sub Command7_Click()
Dim strUSER As String
strUSER = Nz(DLookup("[user]", "table2", "[user] =''" & username.Value & "''"), "nouser")
If strUSER = "nouser" Then
INSERT INTO table2;
VALUES (username.Value,password.Value);
MsgBox "User added"
Else
MsgBox "user already exist"
End If

推荐答案

尝试执行查询如:


如果strUSER =" nouser"然后

currentdb.execute(" INSERT INTO table2(用户名,密码)VALUES(''"& me.Username&"'',''"& me.txtPassword &"'');

MsgBox" User added"

else


VBA不具备处理SQL语法。需要构建查询,然后执行,例如使用docmd.runSQL或上面的currentdb.execute。

INSERT语句需要我看到的一些研究,然后用图形查询来尝试它们编辑器,然后将用于字符串的SQLtext移动到您的代码。


Nic; o)
Try to execute the query like:

If strUSER = "nouser" Then
currentdb.execute ("INSERT INTO table2 (username, password) VALUES (''" & me.Username & "'',''" & me.txtPassword & "'') ;
MsgBox "User added"
else

VBA isn''t capable of handling SQL syntax. The query will need to be constructed and then executed e.g. with docmd.runSQL or the above currentdb.execute.
The INSERT statement needs some study I see, firt try them with the grapical query editor and then move the SQLtext for stringing to your code.

Nic;o)



尝试执行以下查询:


如果strUSER =" nouser"然后

currentdb.execute(" INSERT INTO table2(用户名,密码)VALUES(''"& me.Username&"'',''"& me.txtPassword &"'');

MsgBox" User added"

else


VBA不具备处理SQL语法。需要构建查询,然后执行,例如使用docmd.runSQL或上面的currentdb.execute。

INSERT语句需要我看到的一些研究,然后用图形查询来尝试它们编辑器,然后将用于字符串的SQLtext移动到您的代码。


Nic; o)
Try to execute the query like:

If strUSER = "nouser" Then
currentdb.execute ("INSERT INTO table2 (username, password) VALUES (''" & me.Username & "'',''" & me.txtPassword & "'') ;
MsgBox "User added"
else

VBA isn''t capable of handling SQL syntax. The query will need to be constructed and then executed e.g. with docmd.runSQL or the above currentdb.execute.
The INSERT statement needs some study I see, firt try them with the grapical query editor and then move the SQLtext for stringing to your code.

Nic;o)



不幸的是代码生成语法错误:(

Unfortunately the code generated syntax error :(



不幸的是代码生成语法错误:(
Unfortunately the code generated syntax error :(



I尝试过这个


CurrentDb.Execute(" INSERT INTO table2([user],[pass])VALUES(username.value,password.value)")


gener ates:参数太少,期望2


我也试过

CurrentDb.Execute(" INSERT INTO table2([user],[pass])VALUES (''username.value'',''password.value'')")


事情被添加到表格中,而不是用户名我实际获得文本用户名表中的值。


第3次尝试


CurrentDb.Execute(&INSERT INTO table2([user],[pass] ])VALUES(''' &安培; username.value& "","" &安培; password.value& "'''")")


会在第一个时产生错误''"


I tried this

CurrentDb.Execute ("INSERT INTO table2 ([user], [pass]) VALUES (username.value,password.value)")

generates: too few parameters, expect 2

I also tried
CurrentDb.Execute ("INSERT INTO table2 ([user], [pass]) VALUES (''username.value'',''password.value'')")

Things get added to the table but instead the value of username I actualy get the text username.value in the table!!!

3rd attempt

CurrentDb.Execute ("INSERT INTO table2 ([user], [pass]) VALUES ("''" & username.value & "''","''" & password.value & "''")")

would generate error at the first " '' "


这篇关于使用VBA插入查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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