2

我必须制作一个可以跟踪 之后的字段的审计跟踪INSERT INTO,但是,我无法获取“值”中的表达式表示的任何变量进入字段,只有VALUES. 例如,两个带有数值的条目给了我数值,其他的给了我一个字符串,例如Form.RecordSource = clientName

db.Execute "INSERT INTO tblAudit ([RecordID], [AuditWho], [AuditWhen], [AuditWhat],     [AuditFrom], [AuditTo]) VALUES ('1', '2', 'Now()', 'Form.RecordSource = clientName' , ' Me.ClientName.Value ', '  Me.clientNameTextBox.Value  ')"
4

2 回答 2

3

至少你需要做这样的事情:

Dim SQL as string

SQL = "INSERT INTO tblAudit ([RecordID], [AuditWho], [AuditWhen], [AuditWhat],     [AuditFrom], [AuditTo]) VALUES ('1', '2','" & Now() & "', 'ClientName' , '" & Me.ClientName.Value & "', '" & Me.clientNameTextBox.Value & "')"

db.Execute SQL

为了更“健壮”,您应该在尝试执行插入之前为AuditFromAuditTo值声明变量并验证输入的数据是否“正常”:

Dim SQL as string
dim strAuditFrom as string
Dim strAuditTo As String

strAuditFrom = Me.ClientName.Value & ""
strAuditTo = Me.ClientNameTextBox.Value & ""

if strAuditTo = vbNullString then
  'Alert the user or throw an error perhaps?
end if

sql = "INSERT INTO tblAudit ([RecordID], [AuditWho], [AuditWhen], [AuditWhat],   [AuditFrom], [AuditTo]) VALUES ('1', '2','" & Now() & "', 'ClientName' , '" &strAuditFrom & "', '" & strAuditTo & "')"

db.Execute sql
于 2012-10-24T20:35:48.627 回答
1

您正在使用单个 qoutes 定义要插入的文本。要从控件获取值并将其包含在查询中,您需要使用双引号对字符串进行转义,将字符串与与符号连接,找到控件的值,然后使用与符号和双引号继续字符串。例如:

db.Execute "INSERT INTO tblOne (Field1, Field2) VALUES ('literal text','" & ctlSomething & "')"
于 2012-10-24T20:38:49.810 回答