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