0

我是 vb 的新手,并试图通过搜索网络或询问同事来解决问题,但现在我遇到了死胡同。我想让我的程序确保在保存到数据库之前我的所有文本框都已填充。

这是我的代码:

Private Sub CmdSave_Click()

Set rs = New ADODB.Recordset
    With rs
    .Open "Select * from table1", cn, 2, 3
        If LblAdd_Edit.Caption = "ADD" Then
    If MsgBox("Do you want to save this new rocord?", vbQuestion + vbYesNo, "FJD Inventory") = vbNo Then: Exit Sub
    .AddNew
        !Type = TxtName.Text
    !System = txtsys.Text
    !acc = TxtAcc.Text
    !owner = TxtOwn.Text
    !dept = TxtDpt.Text
    !svctag = txtSvcTag.Text
    .Update
    Else
    If MsgBox("Do you want to save this changes?", vbQuestion + vbYesNo, "FJD Inventory") = vbNo Then: Exit Sub
    Do While Not .EOF

    If LvList.SelectedItem.Text = !Type Then
        !Type = TxtName.Text
        !System = txtsys.Text
        !acc = TxtAcc.Text
        !owner = TxtOwn.Text
        !dept = TxtDpt.Text
        !svctag = txtSvcTag.Text
        .Update

    Exit Do
    Else
    .MoveNext
    End If
    Loop
    End If


    End With

    Form_Activate
    Save_Cancel

End Sub

我试图添加以下内容

If TxtName.Text = "" Or txtsys.Text = "" Or TxtAcc.Text = "" Or TxtOwn.Text = "" Or TxtDpt.Text = "" Or txtSvcTag.Text = "" Then
    MsgBox("All Fields Required", vbCritical, "Error") = vbOK: Exit Sub

当我运行程序时,我得到一个编译错误函数或赋值左侧的调用必须返回一个变量或对象。我一直使用那个 msgbox 函数,但现在它出现了错误

4

3 回答 3

2
If TxtName.Text = "" Or txtsys.Text = "" Or TxtAcc.Text = "" Or TxtOwn.Text = "" Or TxtDpt.Text = "" Or txtSvcTag.Text = "" Then
    If MsgBox("All Fields Required", vbCritical, "Error") = vbOK Then Exit Sub
于 2013-10-16T12:39:23.647 回答
0

这是一个通用的解决方案。它使用一个函数来检查表单上的每个文本框并演示如何使用该函数。我还将文本长度而不是文本与空字符串进行比较,因为(通常)数字比较比字符串比较快。

Private Sub Command1_Click()

    If ValidateTextFields Then
        MsgBox "Your changes have been saved."
    Else
        MsgBox "All fields are required."
    End If

End Sub

Private Function ValidateTextFields() As Boolean
    Dim ctrl As Control
    Dim result As Boolean

    result = True        'set this to false if a textbox fails
    For Each ctrl In Me.Controls
        If TypeOf ctrl Is TextBox Then
            If Len(ctrl.Text) = 0 Then
                result = False
                Exit For 'bail on the first failure
            End If
        End If
    Next ctrl
    ValidateTextFields = result

End Function
于 2013-10-16T16:51:14.623 回答
0

在 VB6 中,您可以使用Trim()函数使空格不被视为字符。

If (Trim$(txtGOSID.Text) = "") Then
msgBox "Please provide input.", vbExclamation

$符号,直接Trim()返回String值;不带$ 符号,Trim()返回Variant带有子类型 String 的 a。

于 2018-07-20T08:56:53.087 回答