Clear a form

Pace

Slick!
Local time
Today, 22:09
Joined
Jan 6, 2005
Messages
18
Hey all,

I have some code that looks like this to control a form;

Code:
Private Sub btnAdd_Click()
    Dim UserName As String
    Dim Initials As String
    Dim Password As String
    Dim OutlookName As String
    Dim rst As DAO.Recordset

'Check each control, is their a value? if not, set focus to control
If IsNull(txtUserName) Then
    MsgBox "You did not enter a new UserName nobby!"
    Me!txtUserName.SetFocus
    Exit Sub
    
ElseIf IsNull(txtInitials) Then                         ' return value of UserName variable;
    MsgBox "You have not entered any initials for user: '" & Me!txtUserName & "'"
    Me!txtInitials.SetFocus
    Exit Sub
    
ElseIf IsNull(txtPassword) Then
    MsgBox "You must create a password for user: '" & Me!txtUserName & "'"
    Me!txtPassword.SetFocus
    Exit Sub
    
ElseIf IsNull(txtOutlookName) Then
    MsgBox "You must enter a Outlook name for: '" & Me!txtUserName & "'"
    Me!txtOutlookName.SetFocus
    Exit Sub
    
End If
' Pass the variables to the table.
Set rst = CurrentDb.OpenRecordset(("Users"), dbOpenDynaset, [dbSeeChanges])
    With rst
    .AddNew
    ![User] = Me!txtUserName
    ![Password] = Me!txtPassword
    ![Initials] = Me!txtInitials
    ![OutlookName] = Me!txtOutlookName
    ![Level] = 1
    ![Select] = 0
    ![dummy] = Null
    .Update
    .Close
Set rst = Nothing
    End With
    DoCmd.Close
End Sub

Private Sub btnCancel_Click()
' Confirm Cancellation Box
    If MsgBox("Are you sure you want to quit?", vbYesNo, "Caution") = vbYes Then
        DoCmd.Close
    Else
     DoCmd.CancelEvent
    End If

End Sub

What I really want to do is once the update has occured is set a label I have as hidden, to show and to clear all the controls.
 
Why do you need all that to clear a form's controls?
Why not just bind the form to a table?
 
I dont need all that to clear the controls :( I want to clear the controls afterwards and let my user know that the data in the recordset has been added ;)

Im new to all this so I am just making bits and bats here and their and having a good play integrating them into the larger application. Ok I have a form that displays my user information that is bound.

Now in order to get some experience "doing things" I thought I would make a button linked to a new form that allowed me to add a user. The bound form does not allow for edits (which is what I want.)

Ok so when a user clicks this button my form with the code shown here opens and they fill in the more eye candy controls and hit the button to add the user.

I will then make another to modify a user, you may well say why would you want to do that and you could do this better and what not but I just want to learn by getting my hands dirty :cool: :p
 
You certainly don't need two forms.

By using the DoCmd.OpenForm method you can specify whether the form is for adding records or goes to a specific record, and whether edits are or are not allowed.

I'd suggest reading about this method and also looking at the form's Current event.
 
hmm ok I see what you are saying. I know that this is NOT the best way to do things, I just sat up and thought hey ill do that... so now I have started I really want to :confused:
 
Hey I got it, prob not the best way to do it, but hey it works and on the plus side I did the lot me self egh ;)

If anyone is interested I have posted the edited code;

Code:
Private Sub btnAdd_Click()
    Dim UserName As String
    Dim Initials As String
    Dim Password As String
    Dim OutlookName As String
    Dim rst As DAO.Recordset

'Check each control, is their a value? if not, set focus to control
If IsNull(txtUserName) Then
    MsgBox "You did not enter a new UserName nobby!"
    Me!txtUserName.SetFocus
    Exit Sub
    
ElseIf IsNull(txtInitials) Then                         ' return value of UserName variable;
    MsgBox "You have not entered any initials for user: '" & Me!txtUserName & "'"
    Me!txtInitials.SetFocus
    Exit Sub
    
ElseIf IsNull(txtPassword) Then
    MsgBox "You must create a password for user: '" & Me!txtUserName & "'"
    Me!txtPassword.SetFocus
    Exit Sub
    
ElseIf IsNull(txtOutlookName) Then
    MsgBox "You must enter a Outlook name for: '" & Me!txtUserName & "'"
    Me!txtOutlookName.SetFocus
    Exit Sub
    
End If
' Pass the variables to the table.
Set rst = CurrentDb.OpenRecordset(("Users"), dbOpenDynaset, [dbSeeChanges])
    With rst
    .AddNew
    ![User] = Me!txtUserName
    ![Password] = Me!txtPassword
    ![Initials] = Me!txtInitials
    ![OutlookName] = Me!txtOutlookName
    ![Level] = 1
    ![Select] = 0
    ![dummy] = Null
    .Update
    .Close
    End With
    
Set rst = Nothing

lblAdded.Visible = True

 If MsgBox("The user: '" & Me!txtUserName & "' was successfully added. Do you wish to add another?", _
    vbYesNo, "Information") = vbYes Then
        DoCmd.Close
        DoCmd.OpenForm "Add_User", , , , , acDialog
    Else
        DoCmd.Close
    End If
End Sub

Private Sub btnCancel_Click()
' Confirm Cancellation Box
    If MsgBox("Are you sure you want to quit?", vbYesNo, "Caution") = vbYes Then
        DoCmd.Close
    Else
        DoCmd.CancelEvent
    End If

End Sub
 

Users who are viewing this thread

Back
Top Bottom