|
-
Jun 18th, 2005, 02:23 PM
#1
Thread Starter
Frenzied Member
Problem with cancel and next button in form
Hi guys. I got a unbounded form that is used to update/delete/add records. It also has navigation buttons called next and previous. I have the following problem and do not know how to fix it. I be happy if some one help me fix them. Thanks
http://i5.photobucket.com/albums/y18...7/editform.jpg
===> Picture of unbounded form
1) When I press add button and if I try not to add and press cancel. The old value does not get posted back into text box! But if I click on edit button and try not edit and press cancel button the old value get posted. I want the old values get posted to text box when a user presses cancel after he clicked on add button.
2) When I click on edit or add button then if I press next it tells me to either save or cancel. When I press on cancel button and then press next again. I get same prompt asking me to either save or cancel again!
Code:
Option Compare Database
Option Explicit
Sub clearTextBoxes()
''clearing the tow texboxes txtCompanyName and txtCustomerId
Me.customerNumber.Value = ""
Me.customerName.Value = ""
End Sub
Sub getReadyForAnAddOperation()
Me.cmdSave__.Enabled = True
Me.cmdSave__.SetFocus
Me.cmdCancel.Enabled = True
Me.cmdAdd__.Enabled = False
Me.cmdEdit.Enabled = False
Me.cmdDelete.Enabled = False
End Sub
Sub stateOnLoad()
Me.customerName.SetFocus
Me.cmdCancel.Enabled = True
Me.cmdSave__.Enabled = False
Me.cmdEdit.Enabled = True
Me.cmdAdd__.Enabled = True
'''disableing the cancel and save button on load
'''Me.cmdCancel.Enabled = False
'''Me.cmdSave__.Enabled = False
End Sub
'declaring subrotine
Sub FillFeilds()
Me.customerNumber = myRS.Fields("customerno")
Me.customerName = myRS.Fields("customername")
End Sub
Private Sub cmdAdd___Click()
clearTextBoxes
getReadyForAnAddOperation
pbAddingARecord = True
'''changing the value of this boolean variable
myRS.AddNew
End Sub
Private Sub cmdCancel_Click()
FillFeilds
stateOnLoad
End Sub
Private Sub cmdDelete_Click()
Dim x As Variant
x = MsgBox(" You are abut to delete " & Me.customerName & " from this table - proceed ? ", vbOKCancel)
If x = 1 Then
With myRS
.Delete
.MoveFirst
FillFeilds
stateOnLoad
End With
End If
End Sub
Private Sub cmdEdit_Click()
''clearTextBoxes
getReadyForAnAddOperation
pbEditingARecord = True
'''changing the value of this boolean variable
End Sub
Private Sub cmdMoveFirst_Click()
myRS.MoveFirst
FillFeilds
End Sub
Private Sub cmdMoveLast_Click()
myRS.MoveLast
FillFeilds
End Sub
Private Sub cmdMoveNext_Click()
If pbAddingARecord = True Or pbEditingARecord = True Then
MsgBox ("Please save or cancel changes first ")
Exit Sub
End If
myRS.MoveNext
If myRS.EOF Then
MsgBox ("Last Record")
myRS.MovePrevious
End If
FillFeilds
End Sub
Private Sub cmdMovePreviouse_Click()
myRS.MovePrevious
If myRS.BOF Then
MsgBox (" First record")
myRS.MoveNext
End If
FillFeilds
End Sub
Private Sub cmdSave___Click()
If pbAddingARecord = True Then
myRS.AddNew
End If
If pbEditingARecord = True Then
myRS.Edit
End If
'''inserting the value of textboxes to the table fields.feeding the date to record set
myRS.Fields("customerno").Value = Me.customerNumber.Value
myRS.Fields("customername").Value = Me.customerName.Value
'''calling update method. it comittes the changes
myRS.Update
pbAddingARecord = False
pbEditingARecord = False
stateOnLoad
End Sub
Private Sub Form_Load()
Set db = CurrentDb()
'''Set myRS = db.OpenRecordset("select * from customer")
Set myRS = db.OpenRecordset("customer", dbOpenTable)
''' need to learn how to add index to customer table
'''myRS.Index = ("Company Name")
'calling subroutine
stateOnLoad
FillFeilds
End Sub
Private Sub lblFindIt_Click()
With myRS
Select Case Me.lblFindIt.Caption
Case "Company Name"
Me.lblFindIt.Caption = " First Name "
''' this indexcontactfirstname should already exist in the table
''' you can also create index trough code on tables. this method
''' not good in multi user evironment . best way to create indexes and
''' refere them trough code
.Index = "CotactFirstName"
Case "First Name"
Me.lblFindIt.Caption = " Last Name "
.Index = "CotactLastName"
Case "Last Name "
Me.lblFindIt.Caption = "Company Name "
.Index = "CompanyName"
End Select
End With
End
End Sub
Private Sub textFindIt_Change()
Dim strSeek As Variant
Dim posInmyRS As Variant
''' feeding it the value from text box
strSeek = Me![textFindIt].Text
With myRS
posInmyRS = myRS.Bookmark
.Seek ">=", strSeek
If .NoMatch = True Then
myRS.Bookmark = posInmyRS
Exit Sub
End If
FillFeilds
End With
End Sub
my modul code
Code:
Option Compare Database
Option Explicit
'''we declare publick variable in the module
'''note we can oly declare public ariables insdie module
'''not inside the function
''' we declare it here so that fillfeilds sub routine can see it.
''' we can not declare it public inside onload
Public db As Database
Public myRS As Recordset
Public pbEditingARecord As Boolean
Public frmName As String
Public ctrlName As String
Public pbAddingARecord As Boolean
Public pbCurrentPos As Variant
-
Jun 18th, 2005, 09:23 PM
#2
Re: Problem with cancel and next button in form
#2 in command save you need to reset you boolean to false so your other buttons know it has finished update or add
you can also add that code after the messagebox
if msgbox("blah") = vbCancel then pbEditingARecord = false
#1 when you press add you need to have a variable to keep the current record, so that if you press cancel, you can o back to it as after you myRS.AddNew you set the recordset to a new blank record, you could also use the recordset bookmark to do this.
cmd_cancel should also cancel the myRS.AddNew by myRS.CancelUpdate,
though really you should not have that in cmdadd as it is in cmdsave
remove the myRS.addnew from cmdadd
pete
-
Jun 20th, 2005, 10:07 AM
#3
Thread Starter
Frenzied Member
Re: Problem with cancel and next button in form
 Originally Posted by westconn1
#2 in command save you need to reset you boolean to false so your other buttons know it has finished update or add
you can also add that code after the messagebox
if msgbox("blah") = vbCancel then pbEditingARecord = false
#1 when you press add you need to have a variable to keep the current record, so that if you press cancel, you can o back to it as after you myRS.AddNew you set the recordset to a new blank record, you could also use the recordset bookmark to do this.
cmd_cancel should also cancel the myRS.AddNew by myRS.CancelUpdate,
though really you should not have that in cmdadd as it is in cmdsave
remove the myRS.addnew from cmdadd
pete
2# Thank u for u reply.well i already set my boolen to false in the above code .(pbAddingARecord = False ,pbEditingARecord = False)
Code:
pbAddingARecord = False
pbEditingARecord = False
Code:
Private Sub cmdSave___Click()
If pbAddingARecord = True Then
myRS.AddNew
End If
If pbEditingARecord = True Then
myRS.Edit
End If
'''inserting the value of textboxes to the table fields.feeding the date to record set
myRS.Fields("customerno").Value = Me.customerNumber.Value
myRS.Fields("customername").Value = Me.customerName.Value
'''calling update method. it comittes the changes
myRS.Update
pbAddingARecord = False
pbEditingARecord = False
stateOnLoad
End Sub
Furthermore where should i place this code :
Code:
if msgbox("blah") = vbCancel then pbEditingARecord = false
1#
could u show me how to create variable to keep the current record.
do u mean should i set myRS.AddNew by myRS.CancelUpdate to false ?
i be happy if u explain me a little more.Thanks
-
Jun 20th, 2005, 11:41 PM
#4
Re: Problem with cancel and next button in form
2# Thank u for u reply.well i already set my boolen to false in the above code .(pbAddingARecord = False ,pbEditingARecord = False)
but you are only doing when you save, you need to do when you cancel too
do u mean should i set myRS.AddNew by myRS.CancelUpdate to false
you can cancel myrs.addnew by myrs.cancelupdate
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|