2011-10-01 37 views
4

我有一個簡單的MS Access表單,它有3個對象:一個文本框,一個列表框和一個按鈕。該表單的用途如下:用戶在文本框中輸入名稱,從列表框中選擇一個項目,然後單擊按鈕將數據添加到表格中。MS Access:我的表單中有一個錯誤

但是,當我點擊按鈕時,我不斷收到錯誤消息,「除非控件具有焦點,否則不能引用屬性或控件的方法。」

以下是我的代碼。謝謝!

Private Sub addRecord_button_Click() 
    Dim CustomerName As String 
    Dim CustomerType As String 

    On Error GoTo Errhandler 

    CustomerName = "[name not selected]" 
    CustomerType = "[type not selected]" 

    CustomerName = Customer_TextBox.Text 

    Select Case Type_ListBox.ListIndex 
     Case 0 
      CustomerType = "Type 1" 
     Case 1 
      CustomerType = "Type 2" 
     Case 2 
      CustomerType = "Type 3" 
    End Select 

    'MsgBox ("Name: " & CustomerName & " and Type: " & CustomerType) 

    DoCmd.RunSQL "INSERT INTO Customer VALUES (CustomerName, CustomerType);" 

Errhandler: 
    MsgBox ("The following error has occured: " & Err & " - " & Error(Err)) 
End Sub 

回答

8

.Text屬性只能在TextBox有焦點時使用。嘗試使用.Value屬性代替。

嘗試用

CustomerName = Customer_TextBox.Value 
+2

替換以下行

CustomerName = Customer_TextBox.Text 

或者,完全省去了一個屬性,即'我!Customer_Textbox'(也真的應該指定父形式,即,我!因爲它區別於一個變量的引用)。 –