【问题标题】:Looping through table to find value from a textbox循环遍历表格以从文本框中查找值
【发布时间】:2020-06-19 13:48:49
【问题描述】:

我正在尝试从 Access 中的表单中遍历表中的列,以查明“案例名称”是否已存在,如果不存在,则将新记录添加到表中。我希望标准基于文本框的输入值。好消息是我已经知道如何使用下面的代码向表中添加新记录。我只是停留在如何遍历表以找出记录是否已经存在。提前致谢!

Private Sub SaveNewCase_Click()

If Me.txtNewCaseName.Value <> "Null" And Me.txtCaseDepth.Value <> "Null" And Me.txtCaseHeight2.Value <> "Null" And Me.txtCaseWeight.Value <> "Null" And Me.txtCaseWidth <> "Null" And Me.cboCaseCategory.Value <> "Null" Then
    'I think the loop should go here, but not sure'
    CurrentDb.Execute "INSERT INTO tblCases(CaseName, CaseWidth, CaseHeight, CaseCasters, CaseWeight, CaseDepth, CaseCategory) " & _
        " VALUES ('" & Me.txtNewCaseName & "'," & Me.txtCaseWidth & "," & Me.txtCaseHeight2 & ",'" & Me.chkboxCasters & "'," & Me.txtCaseWeight & "," & Me.txtCaseDepth & ",'" & Me.cboCaseCategory & "')"
Else
    MsgBox "Please enter all new case criteria."
End If

End Sub

【问题讨论】:

  • 不需要循环。使用 DCount。
  • 如果您将CaseName设为主键或唯一索引,您不必再担心,因为在重复插入/更新时会引发错误

标签: sql vba forms loops ms-access


【解决方案1】:

首先,使用参数

将用户提供的值直接连接到您的 SQL 语句中会使您暴露于SQL injection,无论是有意的(即用户输入自己的 SQL 语句来破坏您的数据库)还是无意的(例如用户输入的值包含撇号或其他 SQL 分隔符) .

改为用参数表示每个字段值,例如:

With CurrentDb.CreateQueryDef _
    ( _
        "", _
        "insert into " & _
        "tblcases (casename,  casewidth,  caseheight,  casecasters,  caseweight,  casedepth,  casecategory) " & _
        "values  (@casename, @casewidth, @caseheight, @casecasters, @caseweight, @casedepth, @casecategory) " _
    )
    .Parameters("@casename") = txtNewCaseName
    .Parameters("@casewidth") = txtCaseWidth
    .Parameters("@caseheight") = txtCaseHeight2
    .Parameters("@casecasters") = chkboxCasters
    .Parameters("@caseweight") = txtCaseWeight
    .Parameters("@casedepth") = txtCaseDepth
    .Parameters("@casecategory") = cboCaseCategory
    .Execute
End With

由于每个表单控件的值直接提供给 SQL 语句中的参数,因此该值将始终被解释为文字,不能构成 SQL 语句本身的一部分。

此外,您不必担心用单引号或双引号将字符串值括起来,也不必担心格式化日期值 - 数据以其本机形式使用。


在测试现有值时,您可以使用域聚合函数,例如DLookup,也可以使用 SQL select 语句并测试没有返回记录,例如:

Dim flg As Boolean
With CurrentDb.CreateQueryDef _
    ( _
        "", _
        "select * from tblcases where " & _
        "casename     = @casename    and " & _
        "casewidth    = @casewidth   and " & _
        "caseheight   = @caseheight  and " & _
        "casecasters  = @casecasters and " & _
        "caseweight   = @caseweight  and " & _
        "casedepth    = @casedepth   and " & _
        "casecategory = @casecategory " _
    )
    .Parameters("@casename") = txtNewCaseName
    .Parameters("@casewidth") = txtCaseWidth
    .Parameters("@caseheight") = txtCaseHeight2
    .Parameters("@casecasters") = chkboxCasters
    .Parameters("@caseweight") = txtCaseWeight
    .Parameters("@casedepth") = txtCaseDepth
    .Parameters("@casecategory") = cboCaseCategory
    With .OpenRecordset
        flg = .EOF
        .Close
    End With
End With

If flg Then
    ' Add new record
Else
    ' Record already exists
End If

最后,您当前正在针对文字字符串"Null" 测试表单控件的值,只有当用户在控件中输入值Null 时才会验证该值,而不是当控件为空白时。

相反,您应该使用 VBA IsNull 函数来检查变量是否包含 Null 值。

【讨论】:

  • 考虑添加我对使用唯一索引的评论,因为这样可以防止欺骗,无论他们试图添加到哪里。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-17
  • 2011-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多