how to close a form when using Access.

S

sheena

I have created a form in Access 2002 when the user clicks on either of the
buttons on the form which both open new pagesi would like the page to close.

i looked at the coding attached to the button and this is what it read.

Private Sub Enter_pg1_Click()
On Error GoTo Err_Enter_pg1_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Data_Capture_step_2"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_Enter_pg1_Click:
Exit Sub
Err_Enter_pg1_Click:
MsgBox Err.Description
Resume Exit_Enter_pg1_Click
End Sub


i think the code to close a form is

DoCmd.OpenQuery "query name", acViewNormal, acReadOnly
DoCmd.Close acForm, "form name"

is this coorect?

And if so where abouts would tihis need to be entered into the above code so
that my form will close after it has carried out the fuctions it needs to.
 
P

Paolo

Hi Sheena,

To close a form the DoCmd.Close acForm, "form name" is 'nuff.
In your code you can put it after the DoCmd.OpenForm stDocName, , ,
stLinkCriteria
Just one question: why do you use strLinkCriteria (actually is for passing
parameters to the opening form) if you don't write nothing in it?

HTH Paolo
 
S

sheena

i created the form using wizard so that part of the code was already there i
am new to VB and dont't understand some of the coding. I'm not actually sure
what that part of the coding means or what it does.

if you could explain that would be great.

thank you
 
P

Paolo

Well Sheena,
this is a little explanation of your code

Private Sub Enter_pg1_Click()
On Error GoTo Err_Enter_pg1_Click ''this is to trap errors. If an error is
raised the execution will continue from Err_Enter_pg1_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Data_Capture_step_2" 'this is the variable where you put
the name of the form you wanna open but is redundant 'cause you can write it
in the line with the openform i.e.
DoCmd.OpenForm "Data_Capture_step_2", , , stLinkCriteria so this line can
substitute the following

DoCmd.OpenForm stDocName, , , stLinkCriteria
In place of stLinkCriteria you can put a where condition so the data in the
form you are opening are filtered with the conditions you specify
Exit_Enter_pg1_Click:
Exit Sub
Err_Enter_pg1_Click: this is the error handler. It display the desciption of
the error and then continue the execution from Exit_Enter_pg1_Click that
actually simply exit the execution
MsgBox Err.Description
Resume Exit_Enter_pg1_Click
End Sub

All your code can be written more simply in this way:

DoCmd.OpenForm "Data_Capture_step_2"
The above statement open the form Data_Capture_step_2.
If you wanna open it with a condition:
DoCmd.OpenForm "Data_Capture_step_2", , ,stLinkCriteria
where stLinkCriteria is a string expression that's a valid SQL WHERE clause
without the word WHERE. Obviously between quotation marks(")

Cheers Paolo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top