Dao

vbnt

Registered User.
Local time
Tomorrow, 01:33
Joined
Sep 28, 2006
Messages
20
Hi all

Can any one check if this code correct please?
Thank you

Private Sub CmdAdd_Click()

Dim db As DAO.Database
Dim rst As DAO.Recordset

Set db = CurrentDb
Set rst = db.OpenRecordset("table1", dbOpenDynaset)

If IsNull(ComboName) Then
'MsgBox "Please Select comboname", vbOKOnly + 64, "Save

DoCmd.CancelEvent
txtComboName.SetFocus


ElseIf MsgBox("Do you like to save?", vbQuestion + vbYesNo, " Save") = vbYes Then

rst.AddNew
rst!Name = ComboName
rst!Amount = txtAmount
rst!Note = txtnote
rst.Update

rst.Requery
Me.Refresh

Me.ComboName = Null
Me.txtAmount = Null
Me.txtnote = Null

rst.Close
ComboName.SetFocus
Else
DoCmd.CancelEvent
Me.ComboName = Null
Me.txtAmount = Null
Me.txtnote = Null
End If
End Sub
 
If you would use the Code Tags (the #) then your code is easier to read.
Code:
Private Sub CmdAdd_Click()

   Dim db As DAO.Database
   Dim rst As DAO.Recordset

   Set db = CurrentDb
   Set rst = db.OpenRecordset("table1", dbOpenDynaset)

   If IsNull(ComboName) Then
      'MsgBox "Please Select comboname", vbOKOnly + 64, "Save

      DoCmd.CancelEvent
      txtComboName.SetFocus


   ElseIf MsgBox("Do you like to save?", vbQuestion + vbYesNo, " Save") = vbYes Then

      rst.AddNew
      rst!Name = ComboName
      rst!Amount = txtAmount
      rst!Note = txtnote
      rst.Update

      rst.Requery
      Me.Refresh

      Me.ComboName = Null
      Me.txtAmount = Null
      Me.txtnote = Null

      rst.Close
      ComboName.SetFocus
   Else
      DoCmd.CancelEvent
      Me.ComboName = Null
      Me.txtAmount = Null
      Me.txtnote = Null
   End If
End Sub
 
Thanks for reply RuralGuy

How I do that plz?
How about the code I posted, is the method correct from A to Z?

Thank you
 
How I do that plz?
When you are posting a message, press the # in the menu bar and put your code between the tags.
How about the code I posted, is the method correct from A to Z?
Does it work? Correct has a lot to do with "Does it work". There are probably dozens of ways to achieve the same results in code and they can all be said to be correct as long as they work.
 
Are you getting errors when you execute this? I rarely use DAO anymore to update records like this (I usually use SQL statements now) but below is how I might write the code for what you are trying to do. If you use SQL, I think your code would be shorter.

Good Luck


Code:
Private Sub CmdAdd_Click()

    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    
    Dim sTable As String
    Dim sField1 As String
    Dim sField2 As String
    Dim sField3 As String
    
    Dim sPrompt As String
    Dim sTitle As String
    Dim iResponse As Integer      
            
    If IsNull(ComboName) Or ComboName = "" Then
        sPrompt = "Please make a selection from the combo box"
        sTitle = "Database Message"
        MsgBox sPrompt, vbExclamation, sTitle
        ComboName.SetFocus
        End Sub
    Else
        sPrompt = "Do you wish to save?"
        sTitle = "Database Message"
        iResponse = MsgBox(sPrompt, vbQuestion + vbYesNo, sTitle)
        
        If iResponse = vbYes Then
            
            sTable = "Table1"
            sField1 = "Name"
            sField2 = "Amount"
            sField3 = "Note"
            
            Set dbs = CurrentDB
            Set rst = dbs.OpenRecordset(sTable)
            
            rst.AddNew
            rst(sField1) = ComboName
            rst(sField2) = txtAmount
            rst(sField3) = txtNote
            rst.Update
            rst.Close
            rst.Requery
            
            Me.Refresh
            Me.ComboName = Null 'or Me.ComboName = ""
            Me.txtAmount = Null 'or Me.txtAmount = ""
            Me.txtNote = Null 'or Me.txtNote = ""

            ComboName.SetFocus
            
            Set dbs = Nothing
            Set rst = Nothing
            
            Exit Sub
        
        ElseIf iResponse = vbNo Then
        
            Me.ComboName = Null 'or Me.ComboName = ""
            Me.txtAmount = Null 'or Me.txtAmount = ""
            Me.txtNote = Null 'or Me.txtNote = ""
            
            Exit Sub
        
        End If
    End If
    
End Sub
 

Users who are viewing this thread

Back
Top Bottom