2017-08-28 2 views
0

私は今少し問題に直面しています。私の問題は、現在、サブフォームの現在のレコードを編集して「更新」をクリックしたときに、前回のレコードを上書きしてしまうことです。 代わりに、編集したデータレコードをサブフォームから追加し、編集中のものと同じPO番号の新しいレコードとしてサブフォームに挿入したいと思います。編集したデータレコードを新しいレコードとしてアクセスフォームに保存する方法は?

以下

は私のコードです:

Private Sub cmdAdd_Click() 
    'when we click on button Add there are two options 
    '1. for insert 
    '2. for update 
If Me.txtID.Tag & "" = "" Then 
    'this is for insert new 
    'add data to table 
    CurrentDb.Execute "INSERT INTO mxd " & _ 
    "(ID,Fabrication,Width,FinishedGoods,Colour, " & _ 
    "LabDipCode,GrossWeight,NettWeight, " & _ 
    "Lbs,Loss,Yds,Remarks,POType,ComboName,GroundColour)" & _ 
    " VALUES(" & Me.txtID & ",'" & Me.txtFabrication & "','" & _ 
    Me.txtWidth & "','" & Me.txtFinishedGood & "','" & _ 
    Me.txtColour & "','" & Me.txtLabDipCode & "','" & _ 
    Me.txtGrossweight & "','" & _ Me.txtNettweight & "','" & _ 
    Me.txtLbs & "','" & Me.txtLoss & "','" & _ Me.txtYds & "','" & _ 
    Me.txtRemarks & "','" & Me.cboPoType "','" & _ 
    Me.txtGroundColour & "','" & Me.txtComboName & "')" 
Else 
    'otherwise (Tag of txtID store the id of student to be modified) 
    CurrentDb.Execute "UPDATE mxd " & _ 
    " SET ID = " & Me.txtID & _ 
    ", Fabrication = '" & Me.txtFabrication & "'" & _ 
    ", Width = '" & Me.txtWidth & "'" & _ 
    ", FinishedGoods = '" & Me.txtFinishedGood & "'" & _ 
    ", Colour = '" & Me.txtColour & "'" & _ 
    ", LabDipCode = '" & Me.txtLabDipCode & "'" & _ 
    ", GrossWeight = '" & Me.txtGrossweight & "'" & _ 
    ", NettWeight = '" & Me.txtNettweight & "'" & _ 
    ", LBS = '" & Me.txtLbs & "'" & _ 
    ", Loss = '" & Me.txtLoss & "'" & _ 
    ", Yds = '" & Me.txtYds & "'" & _ 
    ", Remarks = '" & Me.txtRemarks & "'" & _ 
    ", POType = '" & Me.cboPoType & "'" & _ 
    ", ComboName = '" & Me.txtComboName & "'" & _ 
    ", GroundColour = '" & Me.txtGroundColour & "'" & _ 
    " WHERE ID = " & Me.txtID.Tag 

    End If 

    'clear form 
    cmdClear_Click 
    'refresh data in list on form 
    FormMxdSub.Form.Requery 


End Sub 

Private Sub cmdClear_Click() 
    Me.txtID = "" 
    Me.txtFabrication = "" 
    Me.txtWidth = "" 
    Me.txtFinishedGood = "" 
    Me.txtColour = "" 
    Me.txtLabDipCode = "" 
    Me.txtGrossweight = "" 
    Me.txtNettweight = "" 
    Me.txtLbs = "" 
    Me.txtLoss = "" 
    Me.txtYds = "" 
    Me.txtRemarks = "" 
    Me.cboPoType = "" 
    Me.txtKeywords = "" 
    Me.txtComboName = "" 
    Me.txtGroundColour = "" 

    'focus on ID text box 
    Me.txtID.SetFocus 

    'set button edit to enable 
    Me.cmdEdit.Enabled = True 
    'change caption of button add to Add 
    Me.cmdAdd.Caption = "Add" 
    'clear tag on txtID for reset new 
    Me.txtID.Tag = "" 

End Sub 

Private Sub cmdClose_Click() 
    DoCmd.Close 
End Sub 

Private Sub cmdDelete_Click() 
    'delete record 
    'check existing selected record 
    If Not (Me.FormMxdSub.Form.Recordset.EOF And 
     Me.FormMxdSub.Form.Recordset.BOF) Then 
     'confirm delete 
     If MsgBox("Are you sure you want to delete?", vbYesNo) = vbYes Then 
      'delete now 
      CurrentDb.Execute "DELETE FROM mxd " & _ 
      "where ID = " & Me.FormMxdSub.Form.Recordset.Fields("ID") 
      'refresh data in list 
      Me.FormMxdSub.Form.Requery 
     End If 
    End If 
End Sub 

Private Sub cmdEdit_Click() 
    'check whether there is exists data in list 
    If Not (Me.FormMxdSub.Form.Recordset.EOF And 
     Me.FormMxdSub.Form.Recordset.BOF) Then 
     'get data to text box control 
     With Me.FormMxdSub.Form.Recordset 
      Me.txtID = .Fields("ID") 
      Me.txtFabrication = .Fields("Fabrication") 
      Me.txtWidth = .Fields("Width") 
      Me.txtFinishedGood = .Fields("FinishedGoods") 
      Me.txtColour = .Fields("Colour") 
      Me.txtLabDipCode = .Fields("LabDipCode") 
      Me.txtGrossweight = .Fields("GrossWeight") 
      Me.txtNettweight = .Fields("NettWeight") 
      Me.txtLbs = .Fields("Lbs") 
      Me.txtLoss = .Fields("Loss") 
      Me.txtYds = .Fields("Yds") 
      Me.txtRemarks = .Fields("Remarks") 
      Me.cboPoType = .Fields("POType") 
      Me.txtComboName = .Fields("ComboName") 
      Me.txtGroundColour = .Fields("GroundColour") 
      'store id of student in Tag of txtID in case id is modified 
      Me.txtID.Tag = .Fields("ID") 
       'change caption of button add to update 
      'Me.cmdAdd.Caption = "Update" 
      'disable button edit 
      Me.cmdEdit.Enabled = False 
     End With 
    End If 

End Sub 

答えて

0

あなたは、現在のレコードのデュープを作成するためにRecordsetCloneを使用することができます。より速く、クリーンで何のタグが必要ありません:Me!NameOfYourSubformControl.Form

+0

ありがとうご返信用:もちろん

Private Sub btnCopy_Click() Dim rstSource As DAO.Recordset Dim rstInsert As DAO.Recordset Dim fld As DAO.Field If Me.NewRecord = True Then Exit Sub Set rstInsert = Me.RecordsetClone Set rstSource = rstInsert.Clone With rstSource If .RecordCount > 0 Then ' Go to the current record. .Bookmark = Me.Bookmark With rstInsert .AddNew For Each fld In rstSource.Fields With fld If .Attributes And dbAutoIncrField Then ' Skip Autonumber or GUID field. ElseIf .Name = "SomeFieldToExclude" Then ' Leave field blank. ElseIf .Name = "SomeOtherFieldToExclude" Then ' Leave field blank. Else ' Copy field content. rstInsert.Fields(.Name).Value = .Value End If End With Next .Update ' Go to the new record and sync form. .MoveLast Me.Bookmark = .Bookmark .Close End With End If .Close End With Set rstInsert = Nothing Set rstSource = Nothing End Sub 

、メインフォーム上のボタンを配置する場合、サブフォームを参照してコードMeに置き換えます!上記のコードで、編集を完了した新しいデータ行を追加できますか? – luzz

+0

保存前ではありません。編集を開始する前にコピーしてください。 – Gustav

関連する問題