VBA跳过Access 2010中的语句

时间:2012-04-16 13:02:13

标签: sql vba if-statement conditional-statements ms-access-2010

我试图这样做,以便当用户从组合框中选择一个人时,他们会显示完整的详细信息,但是当条件应该返回true时,一些错误处理如果语句仍然给出错误

Private Sub ComboOwnerID_Change()
Dim SelID As Integer
Dim rs As DAO.Recordset
Dim db As Database
Dim strSQL As String
Dim result As String
SelID = 0
SelID = Me.ComboOwnerID.Text
If Not (SelID = 0) Then
    If Not (SelID = Null) Then
        Set db = CurrentDb
        strSQL = "SELECT * FROM Owners WHERE OwnerID = " + SelID
        Set rs = db.OpenRecordset(strSQL)
        Do While Not rs.EOF
            result = ""
            result = rs!Title + ". "
            result = result + rs!Forname + " "
            result = result + rs!Surname
            rs.MoveNext
        Loop
        Me.lblOwnerName.Caption = result
    Else
        Me.lblOwnerName.Caption = "error"
    End If
Else
    Me.lblOwnerName.Caption = "error"
End If
End Sub

它还没有达到SQL位,所以我不知道这是否有效

1 个答案:

答案 0 :(得分:2)

不要在VBA中使用.text属性,它仅在控件具有焦点时可用。 VBA中的连接符是&不是+。当其中一个字符串为null时使用+可以返回null。

If Not (SelID = Null) Then
    Set db = CurrentDb

你的意思是:

If Not IsNull(SelID) Then
    Set db = CurrentDb