Full Version: Creating/opening a form (sounds simple enough)
UtterAccess Forums > Microsoft® Access > Access Forms
Deano
How do you open/create a form once only.
I have made a button that will open a form and fill in default data but I only need this form to be created once.
This form is generated from its own table and has its own primary key. Therefor every time I press my "create new form"
button I get a new form with a new primary key.
I have another button that can view and amend the form.
Does any of this make sence. Maybe I should go for a lay down.
kuifjexx
Welcome to UA
You are mixing some things ...
From the database window it is possible to create as many new forms as possible even on the same table/query
There is no restrictions .. so if you choose 'create new form' you can make a new form = possibility to show data ...
A created form is listed in the database window, and can only opened once
"I have another button that can view and amend the form."
Which button do you mean?
Greetz from Belgium compute.gif
Luc aka Kuifjexx
Deano
Looks like I'm not explaining myself very well.
I have a table with over 2000 records/users in it, they access this tale via a form. I need to create a button that will open a new form for each user.
HAs this new record has its own table and Primary Key, it would be possible for a single user to create multiple records
in this new form. So I need a button that will open this new form, create a primary key of 1(one) and if pressed again
not to open a new form.
My second button that I will create shall have the ability to amend the info within the newley created form.
See Word doc for picture.
SerranoG
I still don't understand why you want to create a new form to enter new data. Create the form once, then amend the data via this once-created form. Why create a new one? Please explain more.
Deano
If you could have a look at the attached doc it may help.
Odon't know how to explain any more. My little brain is starting to fry.
SerranoG
I downloaded and viewed your document. You seem to be interchanging the words "Form" and "Record" incorrectly. The red box and the green box are really one and the same. You create one FORM and either enter a new RECORD or amend the old RECORD.
When you say
What you really mean is you don't want the user to create a second RECORD with a primary key value of 2. However, the user will use the same FORM to enter RECORD 1 and to later edit RECORD 1.
I think what you have is this. You have a form with underlying data from a main table, say Table A. The form has a subform with underlying data from Table B. Each form is created ONCE. You would like to limit the underlying Table B in the subform to accept one and only one record per each PK in the main Table A. Therefore, Table B will only have one record for each corresponding record in table A. So you want to prevent people from entering a second record for Table B that corresponds to the PK in Table A.
Is this correct?
Deano
Spot on. Well almost. The only slight error is that "Table B" is not a subform of "Table A". "Table B" is a table/form in its own right and has a link criteria back to "Table A" via the Primary Key. Apart from that you understand what I have desperately been trying to get across. I presumed that you would need to have a button to open/create a form with a Primary Key of 1 then another to view that form. But from what you are saying one button will serve both purposes. If so, how do I create said button?
SerranoG
The programing doesn't lie in the making of the forms nor in the button to open them. You make the forms once and the button has a very simple code to it, namely...
!--c1-->
CODE
DoCmd.OpenForm "Your Form Name Here"

The real trick is in the subform's VBA programming. You have to tell the subform (that is based on TableB) that when it opens, that if a record in TableA already exists then set the AllowAdditions property of the subform to FALSE. That is, on the SUBform's ON OPEN event, put this.
CODE
Me.AllowAdditions = Me.NewRecord

What this does is this. When the subform opens, if it's a new record (that is, no data yet), Me.NewRecord is TRUE and therefore Me.AllowEditions is TRUE. You can now add a record (i.e. primary key 1). The next time you open the subform, because the record is there, Me.NewRecord is FALSE and therefore Me.AllowAdditions is FALSE and you cannot add more records. You can still edit and delete that one, but add nothing new.
Also, on the subform's AFTER INSERT event, put this
CODE
Me.AllowAdditions = False

to immediately block people from adding records as soon as you create a new one; and on the subform's AFTER DELETE event, put this
CODE
Me.AllowAdditions = False

to allow someone to add a new record if you deleted the old one just now.
Deano
Didn't work Greg.
Oknow what you are saying Greg.
When I click the button it allows me to put in a new record.
Close the form and I return to were my button is. Check that I am still the same user.
Click it again and yet again opens the form with a new primary key.
I must be having a blonde moment(week).
I understand if you are getting bored with this. I may have to re-think this issue, or am I your new project now?
Deana
FYI
The button that opens a new record contains...
______________________________________________________________________________
Private Sub cmdOpenNewDocumentation_Click()
On Error GoTo Err_cmdOpenNewDocumentation_Click
Dim stDocName As String
Dim stLinkCriteria As String
Dim stOpenArg As String

stDocName = "frmDocumentation"
stOpenArg = "OpenNew"

DoCmd.OpenForm stDocName, , , stLinkCriteria, , , stOpenArg

Exit_cmdOpenNewDocumentation_Click:
Exit Sub
Err_cmdOpenNewDocumentation_Click:
MsgBox Err.Description
Resume Exit_cmdOpenNewDocumentation_Click
End Sub
________________________________________________________________________________
The code for AfterInsert on the form is ...
________________________________________________________________________________
_
Private Sub Form_AfterInsert()
Me.AllowAdditions = False
End Sub
________________________________________________________________________________
_
The code for On Load on the form is ....
________________________________________________________________________________
_
Private Sub Form_Load()
Dim strOpenArgs As String
Dim frm As Form

strOpenArgs = Forms!frmDocumentation.OpenArgs

If strOpenArgs = "OpenNew" Then
Me.AllowDeletions = False
Me.AllowEdits = True

DoCmd.GoToRecord , , acNewRec
Me![txtRefDealID] = Forms![frmEndUsers]![txtRefDealID]

ElseIf strOpenArgs = "OpenOld" Then
DoCmd.GoToRecord , , acFirst
Me.AllowAdditions = False
Me.AllowDeletions = True
Me.AllowEdits = True

End If
End Sub
________________________________________________________________________________
SerranoG
Oops... correction on my previous post. The ON DELETE event should read
!--c1-->
CODE
Me.AllowAdditions = True

Anyhoo, everything looks good except for the use of this stOpenArgs variable. I don't see the need for it and it's mucking up things.
THere are your events cleaned up.
CODE
Private Sub cmdOpenNewDocumentation_Click()
    
    On Error GoTo Err_cmdOpenNewDocumentation_Click
    
    Dim stDocName As String
    
    stDocName = "frmDocumentation"
    DoCmd.OpenForm stDocName
    
Exit_cmdOpenNewDocumentation_Click:
    Exit Sub
    
Err_cmdOpenNewDocumentation_Click:
    MsgBox Err.Description, vbExclamation, "Error!"
    Resume Exit_cmdOpenNewDocumentation_Click
End Sub

CODE
Private Sub Form_AfterInsert()
    Me.AllowAdditions = False
End Sub

CODE
Private Sub Form_Delete(Cancel as Integer)
    Me.AllowAdditions = True
End Sub

Move the code from ON LOAD to ON OPEN.
CODE
Private Sub Form_Open()
    
    Me.AllowAdditions = Me.NewRecord
    Me.AllowEdits = True
    Me.AllowDeletions = True
    
    Me.txtRefDealID = Forms.frmEndUsers.Form.txtRefDealID
End Sub
Deano
Made the changes you stated.
et an error box saying. The OpenForm action was canceled.
SerranoG
My first guesses are that the name of the form is mispelled in the button's ON CLICK procedure or that form doesn't exist.
Deano
The naming convention is fine Greg, and the form does exist.
Omight have to can this project and have a rethink as time scales are getting tight now.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.