An easy VBA question for you old pros.....

  • Thread starter Thread starter Sarah
  • Start date Start date
S

Sarah

Hi All-

I am new to VBA programming, and Im working on a custom application.

I made a form called "AddCustomer". The form just opens with blank fields
allowing the user to type in a new customer's information like name and
address.

I put an 'Add' button on the form, as well as a 'Cancel' button. The cancel
button works (as in not saving the record you just entered) only when the
form is just partially filled out or not at all. However, If I were to fill
in all the fields (all required ones too) then hit cancel, it exits the
form, but it saves that new record in the database!

How come this is doing this? What command do I need to cancel the saving of
the record?

Thanks in advance!!!!

Sarah
Wilkes-Barre, PA
 
Without seeing the code you have for the cancel button's OnClick event, it's
hard to say. But, you could use Me.Undo to cancel all changes to the
current record.
 
Mark-
Here is my code for the cancel button's onclick event. Its just displays a message box asking if you are sure you want to cancel. If you click Yes, it just closes the form. If you click No, it goes back to the first field.

Private Sub CancelButton_Click()
On Error GoTo Err_CancelButton_Click
'Ask user if he meant to cancel.

Dim Message As String
Dim Options As Integer
Dim Choice As Byte

Message = "Are you sure you want to cancel?"
Options = vbQuestion + vbYesNo
Choice = MsgBox(Message, Options)

If Choice = vbYes Then
DoCmd.Close
Else
FName.SetFocus
End If

Exit_CancelButton_Click:
Exit Sub

Err_CancelButton_Click:
MsgBox Err.Description
Resume Exit_CancelButton_Click

End Sub
 
Sarah said:
Hi All-

I am new to VBA programming, and Im working on a custom application.

I made a form called "AddCustomer". The form just opens with blank fields
allowing the user to type in a new customer's information like name and
address.

I put an 'Add' button on the form, as well as a 'Cancel' button. The cancel
button works (as in not saving the record you just entered) only when the
form is just partially filled out or not at all. However, If I were to fill
in all the fields (all required ones too) then hit cancel, it exits the
form, but it saves that new record in the database!

How come this is doing this? What command do I need to cancel the saving of
the record?

Thanks in advance!!!!

Sarah
Wilkes-Barre, PA

Me.Undo

And less of the "old", if you don't mind young Sarah!
 
Sarah,
Try putting Me.Undo right before DoCmd.Close
This should cancel all changes to the current record and close the form

Also, I've gotten into the habit of explicitly telling Access what to close
when I want to close the form. I've seen unexpected things happen when all
I type is DoCmd.Close. Sometimes it will ask if you want to save changes to
the form, sometimes it closes the wrong thing, etc. So when I want to close
the current form, I use:
DoCmd.Close acForm, Me.Name, acSaveNo
instead of just DoCmd.Close. The pieces to this command are: acForm (what
kind of object to close), Me.Name (the name of the object to close [I use
Me.Name so I don't have to know what the current form's name is]), acSaveNo
(don't save any changes to the form's layout/design).


Mark-
Here is my code for the cancel button's onclick event. Its just displays a
message box asking if you are sure you want to cancel. If you click Yes, it
just closes the form. If you click No, it goes back to the first field.

Private Sub CancelButton_Click()
On Error GoTo Err_CancelButton_Click
'Ask user if he meant to cancel.

Dim Message As String
Dim Options As Integer
Dim Choice As Byte

Message = "Are you sure you want to cancel?"
Options = vbQuestion + vbYesNo
Choice = MsgBox(Message, Options)

If Choice = vbYes Then
DoCmd.Close
Else
FName.SetFocus
End If

Exit_CancelButton_Click:
Exit Sub

Err_CancelButton_Click:
MsgBox Err.Description
Resume Exit_CancelButton_Click

End Sub
 
Thanks Mark and Brian! (sorry for the 'old' reference.)

I put the Me.Undo command before the form closed and it worked great!
I knew you guys would come through! =) I will definitely have more newbie
questions im sure, sorry for that! But I'm learning!

Thanks again,
Sarah
 
Oh, I just thought of another quick question. (See how fast?)

When someone enters data into the fields, how do I make sure they all change
to CAPS?
I tried using ">" in the format property of the field, but that doesn't save
it in the database as caps.

Thanks !!!!!! Love you guys.

Sarha
 
Two things:
Since this is a new question, it would have been better to start a new
thread. :-)

Now to the answer...
Copy this function to a module, and then call it in the AfterUpdate event of
any form's control you wish to be saved as caps:
**********code start***********

Function MakeCap(varField As Variant)
MakeCap = UCase(varField)
End Function

*********code end*************
In the AfterUpdate event for the control, use something like:
Me.txtLastName = MakeCap(Me.txtLastName)
 
Put the following code in the field's AfterUpdate event:
Me!NameOfField = UCase(Me!NameOfField)
 
i too am new to access so im curious...

isnt the afterupdate event fired after the record is saved? why wouldnt you
put that code in the beforeupdate event?
 
heh, i answered my own question just after i hit the post button. youre
using the control's event, not the form's.
 
Back
Top