0

我想通过 VBA 将表单上输入的超链接(作为超链接)带到它需要去的表。我正在使用 Access 2010。它一直给我一个 SQL 语句错误。我知道这与超链接# 的符号有关。我不太了解如何处理超链接。我已经阅读了很多论坛帖子,但它们都不同(不同年份),我似乎无法破解他们的例子来满足我的需求。谁能让我知道我做错了什么?谢谢

Private Sub SaveReq_Click()
' 
' Saves the current entry to the database
' Into the TABLE 'pr_req_table'
' 

' Open a connection to the database
dim data_base as Database
set data_base = OpenDatabase(CurrentProject.Path & "\test_database.accdb")

' Grab all information from form
' Add information to pr_req_table
data_base.Execute "INSERT INTO pr_req_table " _
    & "(pr_no, pr_date, pr_owner, pr_link, pr_signed) " _
    & "VALUES (" & pr_num.Value & ", #" &  Format(pr_date.Value, "mm/dd/yyyy") & "#, " _
        &  List22.Value & ", " & "Excel Copy #" & elec_copy.Value & ", " & "Signed Copy #" & sign_copy.Value & ");"

' Close Database connection
data_base.Close

End Sub

提前感谢您的帮助!

弥敦道

4

2 回答 2

3

我将使用如下参数查询:

Sub InsertRecord()
Dim data_base As Database
Set data_base = OpenDatabase(CurrentProject.Path & "\test_database.accdb")

' Grab all information from form
' Add information to pr_req_table
Dim qd As QueryDef
Set qd = data_base.CreateQueryDef("")
qd.sql = "INSERT INTO pr_req_table(pr_no, pr_date, pr_owner, pr_link, pr_signed) " & _
    "values([p1],[p2],[p3],[p4],[p5])"
qd.Parameters("p1").Value = pr_num.Value
qd.Parameters("p2").Value = Format(pr_date.Value, "mm/dd/yyyy")
qd.Parameters("p3").Value = List22.Value
qd.Parameters("p4").Value = "Excel Copy #" & elec_copy.Value
qd.Parameters("p5").Value =  "Signed Copy #" & sign_copy.Value 
qd.Execute

End Sub
于 2012-07-09T21:57:09.203 回答
1

如果你把 sql 分离出来,就更容易找到错误。

' Grab all information from form
' Add information to pr_req_table
sSQL="INSERT INTO pr_req_table " _
    & "(pr_no, pr_date, pr_owner, pr_link, pr_signed) " _
    & "VALUES (" & pr_num.Value & ", #" _
    &  Format(pr_date.Value, "mm/dd/yyyy") & "#, " _
    &  List22.Value & ", " & "Excel Copy #" & elec_copy.Value & ", " _
    & "Signed Copy #" & sign_copy.Value & ");"
data_base.Execute sSQL

您可以看到超链接没有被引用,所有者也没有。

INSERT INTO pr_req_table (pr_no, pr_date, pr_owner, pr_link, pr_signed) 
VALUES (pr_num, #07/09/2012#, List22, Excel Copy
#elec_copy, Signed Copy #sign_copy);

所以:

sSQL="INSERT INTO pr_req_table " _
    & "(pr_no, pr_date, pr_owner, pr_link, pr_signed) " _
    & "VALUES (" & pr_num.Value & ", #" _
    &  Format(pr_date.Value, "mm/dd/yyyy") & "#, '" _
    &  Replace(List22.Value, "'","''") & "', '" _
    & "Excel Copy #" & elec_copy.Value & '",' " _
    & "Signed Copy #" & sign_copy.Value & "');"

您可能应该在 elec_copy 和 sign_copy 上使用 Replace。

于 2012-07-09T21:50:21.250 回答