473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

VBA/Access: How do I "abandon" a record when using linked forms?

Hi I have a slight problem in trying to set up 2 forms linked to
tables.

What I want to do is to inform a control on one form(Apps)
as to the value of a key of a 'new' record entered via a
second(Clients)

In opening the second form I set up certian fields and controls
on it from the first. In doing so however this seemingly creates a
headache in coding for a situation where a user wants to abandon
entry of a new 'client', for example a NewClient button on the first
being selected by mistake for example.

If I set the Cancel in respect of the BeforeUpdate on the clients form
in the event of null fields, my current code will keep the cleints
form on the 'new' record, and will seemingly not allow an 'abort'
or move to another record. If I don't set the Cancel the Update will
quite happily insert 'blank' data into the database as part of the
update.

A related problem is to do with key allocation.
If having setup a 'new' client by means of opening the clients
form, the user abandons entry of that new client, I would ideally
like to reclaim the key allocated. I had previosuly been using
AutoNumbers but in abandoning records the AutoNumber concerned seemed
quite happy to advance even when a record had been abandoned..

So how do I code the various event routines for the form so that I can
both
setup controls/fields on the Clients form from the Applications form ,
avoid
'blank' records going into the database and have some method for
allocating,
deallocating key values?

(The current portions of code in respect of the forms concerned are
below)

FMNT80

Apps_Form:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Form_Apps"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Compare Database

Private Sub cmd_new_Click()
On Error GoTo Err_cmd_new_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Client"
DoCmd.OpenForm stDocName, , , stLinkCriteria
Forms.Item(stDocName).donewentry Me.Form.Name, cmb_client.Name
Forms.Item(stDocName).Controls.Item("cmb_type") = 3
Exit_cmd_new_Click:
Exit Sub

Err_cmd_new_Click:
MsgBox Err.Description
Resume Exit_cmd_new_Click

End Sub

<ENDS>
Clients Form:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Form_Client"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Compare Database
Dim newentry As Boolean
Dim cbForm As String
Dim cbControl As String
Dim alloc As Integer

Public Sub donewentry(cbF As String, cbC As String)
cbForm = cbF
cbControl = cbC
newentry = True
If Not newrec Then DoCmd.GoToRecord , , acNewRec
End Sub

Private Sub cmb_type_BeforeUpdate(Cancel As Integer)
If Not IsNull Then
If abandon = True Then abandon = False
End If
End Sub

Private Sub cmd_close_Click()
On Error GoTo Err_cmd_close_Click
DoCmd.Close

Exit_cmd_close_Click:
Exit Sub

Err_cmd_close_Click:
MsgBox Err.Description
Resume Exit_cmd_close_Click
End Sub

Private Sub Form_AfterInsert()
If newentry Then
abandon = False
Forms.Item(cbForm).Controls.Item(cbControl).Value = txt_id
doexit
End If
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(txt_name) And Not abandon Then
Cancel = True
abandon = True
alloc = alloc - 1
Form_Current
ElseIf abandon Then
Cancel = False
SendKeys ("{ESC}{ESC}")
If newentry Then doexit
End If
End Sub

Private Sub doexit()
On Error GoTo exit_err
DoCmd.Close acForm, Me.Form.Name

exit_exit:
Exit Sub

exit_err:
If Err.Number = 2501 Then
Debug.Print "2501"
Else
MsgBox Err.Number
End If
Resume exit_exit
End Sub

Private Sub Form_Current()
If Form.NewRecord Then
newrec = True
Me![txt_id] = alloc
alloc = alloc + 1
Else
newrec = False
If abandon Then
abandon = False
End If
End If

End Sub

Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 2501 Then Debug.Print "Ignored 2501"
Response = acDataErrContinue
End Sub

Private Sub Form_Load()
Rem Note alloc only set to 10 here as an example
Rem In reality the allocation of a key needs to be done in
Form_Current?

alloc = 10 :
newentry = False
End Sub

Private Sub Form_Unload(uCancel As Integer)
If newentry And abandon Then
If MsgBox("Abandon?", vbYesNo) = vbNo Then
uCancel = True
End If
End If
End Sub

<ENDS>
Nov 13 '05 #1
0 1891

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

63
by: Jerome | last post by:
Hi, I'm a bit confused ... when would I rather write an database application using MS Access and Visual Basic and when (and why) would I rather write it using Visual Studio .Net? Is it as easy...
3
by: Iain Miller | last post by:
I'm sure this ought to be simple but I can't make it work!! I have a form (based on a query) that holds information about an individual. Each individual has a Unique reference field called...
3
by: Colleyville Alan | last post by:
I am constructing a SQL command from a function. Some code builds the WHERE clause in a looping structure and passes that as an argument to the SQL-building function. But the results do not...
0
by: Privacy Trap | last post by:
Hi I have a slight problem in trying to set up 2 forms linked to tables. What I want to do is to inform a control on one form(Apps) as to the value of a key of a 'new' record entered via a...
11
by: olle | last post by:
Hi everyone. I am an Access developer having worked with Access-dev. projects for many years on Sql server databases and Access. For the internet I have been using traditional asp. But now I have...
55
by: AnandaSim | last post by:
I just had a google through this NG but have not seen mention of Erik Rucker's blog entry and the new Jet: http://blogs.msdn.com/access/archive/2005/10/05/477549.aspx mentioned by Mike...
6
by: Mike | last post by:
There is not going to be such a thing as "remoting" 2 years from now. I still see people working with it, and I wonder why they are not keeping up. Windows Communication Foundatoin, aka .Net...
3
prn
by: prn | last post by:
Hi folks, I've got something that's driving me crazy here. If you don't want to read a long explanation, this is not the post for you. My problematic Access app is a DB for keeping track of...
1
by: Dilruba | last post by:
I want the suggestion what is better for use between Session.abandon & Session.Contents.Remove
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.