2010-11-29 56 views
0

我正在使用工作表作爲數據庫表。我有一個用戶表單來填充數據。檢查用戶窗體中的空白文本框以警告消息框

我想編碼下面的場景。

當我單擊用戶窗體上的保存按鈕時,如果用戶窗體中有任何空白文本框,則必須彈出帶有「YES」「NO」按鈕的消息框,並顯示消息,如「客戶名稱不是輸入你想輸入「。

如果我點擊「是」,它必須去空白字段。如果點擊「否」,則必須退出消息框並轉到下一個空白字段(如果有的話)。它必須檢查所有空白字段,然後只將數據保存到工作表中。

我有以下代碼,它將用戶窗體中的日期保存到工作表中的第一個可用空白行,但不檢查用戶窗體中的任何空白文本框。

Private Sub cmdsave_Click() 
    Dim irow As Long 
    Dim ws As Worksheet 
    Set ws = Worksheets("customerDetails") 

    'find first empty row in database 
    irow = ws.Cells(Rows.Count, 3) _ 
    .End(xlUp).Offset(1, 0).Row 


    'save the data to the database 
    ws.Cells(irow, 2).Value = Me.txtcustname.Value 
    ws.Cells(irow, 3).Value = Me.txtinvad1.Value 
    ws.Cells(irow, 4).Value = Me.txtinvad2.Value 
    ws.Cells(irow, 5).Value = Me.txtinvad3.Value 
    ws.Cells(irow, 6).Value = Me.txtdelyad1.Value 
    ws.Cells(irow, 7).Value = Me.txtdelyad2.Value 
    ws.Cells(irow, 8).Value = Me.txtdelyad3.Value 
    ws.Cells(irow, 9).Value = Me.txtcstno.Value 
    ws.Cells(irow, 10).Value = Me.txttinno.Value 
    ws.Cells(irow, 11).Value = Me.txteccno.Value 
    ws.Cells(irow, 12).Value = Me.txtdlno1.Value 
    ws.Cells(irow, 13).Value = Me.txtdlno2.Value 
    ws.Cells(irow, 14).Value = Me.txtstno.Value 
    ws.Cells(irow, 15).Value = Me.txtcsttinno.Value 
    ws.Cells(irow, 16).Value = Me.txtpanno.Value 
    ws.Cells(irow, 17).Value = Me.txtins.Value 


    'clear the data 
    Me.txtcustname.Value = "" 
    Me.txtinvad1.Value = "" 
    Me.txtinvad2.Value = "" 
    Me.txtinvad3.Value = "" 
    Me.txtdelyad1.Value = "" 
    Me.txtdelyad2.Value = "" 
    Me.txtdelyad3.Value = "" 
    Me.txtcstno.Value = "" 
    Me.txttinno.Value = "" 
    Me.txteccno.Value = "" 
    Me.txtdlno1.Value = "" 
    Me.txtdlno2.Value = "" 
    Me.txtstno.Value = "" 
    Me.txtcsttinno.Value = "" 
    Me.txtpanno.Value = "" 
    Me.txtins.Value = "" 


    End Sub 

回答

2

如果您將文本框的敘述名稱(例如「Customer Address」)存儲在控件的標籤屬性中,則可以使用類似下面的內容。

 

Private Sub CommandButton1_Click() 
Dim t As Control, res As VbMsgBoxResult 

For Each t In Me.Controls 
    If TypeName(t) = "TextBox" Then ' Make sure we're only looking at textboxes. 
     If t.Text = vbNullString And t.Tag <> vbNullString Then 
      res = MsgBox("You've not completed the " + t.Tag + " field. Would you like to complete it now?", vbYesNo + vbQuestion) 

      If res = vbYes Then Exit Sub 
    End If 
Next 

End Sub