Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 18th, 2005, 02:11 PM
Friend of Wrox
 
Join Date: Mar 2005
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Paramater value cancel button errors REVNED VB How-To 1 September 2nd, 2008 01:23 PM
DetailsView Cancel button to redirect firblazer ASP.NET 3.5 Basics 4 June 23rd, 2008 01:23 AM
Close all MdiChield form from open one form/Button salman .NET Framework 2.0 6 December 10th, 2007 03:21 AM
How to stop file uploading when a cancel button coolcoder2007 ASP.NET 2.0 Basics 0 October 26th, 2007 04:21 AM
Cancel a keystroke Kenny Alligood Access 4 June 12th, 2003 10:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.