2016-10-02 75 views
0

我試圖在MS Access窗體上創建添加,更新,刪除操作。我在互聯網上發現了這個代碼,其中插入和更新發生在同一個按鈕上。我沒有得到下面發生的事情,以及它是如何識別更新或插入的。MS Access Tag屬性 - 如何識別更新和插入操作?

沒有得到以下行:= Me.txtid.Tag &「」 =「」

請在下面找到它完美的作品按規定的代碼。

「當我們點擊按鈕添加有兩種選擇

'1. for insert 
'2. for update 

If Me.txtid.Tag & "" = "" Then 
    ' this is for insert new 
    ' add data in table 

CurrentDb.Execute "insert into student(stdid,stdname,gender,phone,address)" & _ 
    " values(" & Me.txtid & ",' " & Me.txtname & " ',' " & Me.cmbgender & " ','" & _ 
    Me.txtphone & "', '" & Me.txtaddress & "')" 
'refresh data in list on form 
subform_student.Form.Requery 

否則

CurrentDb.Execute "UPDATE student " & _ 
    " set stdid = " & Me.txtid & _ 
    ", stdname = '" & Me.txtname & "' " & _ 
    ", gender = '" & Me.cmbgender & " ' " & _ 
    ", phone = ' " & Me.txtphone & " ' " & _ 
    ", address = ' " & Me.txtphone & " ' " & _ 
    " WHERE stdid = " & Me.txtid.Tag 

結束如果

回答

4

.Tag property是VBA/VB6中每個窗體和控件對象的通用字符串屬性。它被提供給開發人員「放置東西」以支持其應用程序的運行。

從中複製您的樣品必須具有一定的值寫入Me.txtid.Tag加載記錄時的原代碼(例如,也許在窗體的Current事件)來指示記錄是否是現有記錄或新記錄(空=「新」,非空=「現有」)。行

If Me.txtid.Tag & "" = "" Then 

簡單地檢查以查看是否.Tag屬性是空的,然後相應地進行插入或更新。

BTW,重:

低於完美的作品按規定代碼

不,不。嘗試添加一個[stdname]爲Tam O'Shanter的記錄並親自查看。你應該溝動態SQL和使用的

  • 綁定的形式一個(如古斯塔夫建議),
  • 參數化查詢,或
  • 的記錄更新。
+0

答案是完全有說服力的。感謝您的回覆:) – user3172930

3

忘記/刪除所有代碼和綁定形式表學生自動完成這一切。

如果綁定表格對您不熟悉,請瀏覽「開始使用Microsoft Access」或類似的教程。

+0

請您詳細說明您對Me.txtid.Tag&「」=「」代碼行的迴應嗎? – user3172930

+0

查看答案Gord。 – Gustav