Closing a Window After Button Click

kipster1203

Registered User.
Local time
Today, 03:09
Joined
May 5, 2010
Messages
13
I have a form that opens and prompts user to select a report type, upon clicking on the desired button, it opens the report in a new window.
What I want is for the window to close after the user clicks on a report type, can anyone help me?

Here is the code for the request window

Code:
Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Select Report Form"
DoCmd.OpenForm stDocName, , , stLinkCriteria

So after this I need a code to close the window... thanks
 
So you will want to add your form name to the criteria, but you will see it below

Code:
Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Select Report Form"
DoCmd.OpenForm stDocName, , , stLinkCriteria
 DoCmd.Close acForm, "Your Form Name"

Or you can try
Code:
Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Select Report Form"
DoCmd.OpenForm stDocName, , , stLinkCriteria
 DoCmd.Close acForm, Me.Name
 
These aren't working for me, I am fairly new to coding (2 days), so bare with me! For the first option I tried it as is and got nothing, so I changed the form name to one of the 3 form names I have and still got nothing.
For the second option, am I supposed to replace the 'me'? I also tried it as is and also got nothing, thanks for the help.

So you will want to add your form name to the criteria, but you will see it below

Code:
Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Select Report Form"
DoCmd.OpenForm stDocName, , , stLinkCriteria
 DoCmd.Close acForm, "Your Form Name"

Or you can try
Code:
Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Select Report Form"
DoCmd.OpenForm stDocName, , , stLinkCriteria
 DoCmd.Close acForm, Me.Name
 
Just said that doesn't work... Here's my entire code

Code:
Private Sub Data_Form_Report_Request_Click()
 On Error GoTo Err_Data_Form_Report_Request_Click
    
    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "Select Report Form"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    
    DoCmd.Close acForm, Me.Name
    
Exit_Data_Form_Report_Request_Click:
    Exit Sub

Err_Data_Form_Report_Request_Click:
    MsgBox Err.Description
    Resume Exit_Data_Form_Report_Request_Click
End Sub
 
I used your exact code in a test db and it worked fine for me.

Do you get an error or does nothing happen?
 
Nothing happens it's weird, My window pops up, I select one of 3 reports I have listed, the report pops up, and the selection window stays open.

I just tried DoCmd.Close acForm, Me.OnClick since I want the window to close after the user clicks on a button, idk, but again, it's the same as if I didn't have that code in there, maybe I'm not doing something I'm supposed to after I make the changes? I am just saving my code and then going into access to try running it.... thanks for the input
 
So did you put that command in the "Select Report Form" also?
 
Didn't know I had to... I have no idea where I would place it though, this is the beginning of the Report Form code:

Code:
Option Compare Database

Private Sub Done_Button_Click()
On Error GoTo Err_Done_Button_Click


    DoCmd.Close

Exit_Done_Button_Click:
    Exit Sub

Err_Done_Button_Click:
    MsgBox Err.Description
    Resume Exit_Done_Button_Click
    
End Sub
Private Sub On_Time_Delivery_Report_Click()
On Error GoTo Err_On_Time_Delivery_Report_Click

    Dim stDocName As String

    stDocName = "On-Time Delivery Report"
    DoCmd.OpenReport stDocName, acPreview

Exit_On_Time_Delivery_Report_Click:
    Exit Sub

Err_On_Time_Delivery_Report_Click:
    MsgBox Err.Description
    Resume Exit_On_Time_Delivery_Report_Click
    
End Sub
Private Sub Look_Ahead_Report_Click()
On Error GoTo Err_Look_Ahead_Report_Click

    Dim stDocName As String
 
Ok so if you want the form to close after they select a report then you will have to put this code at the end of each button click so from what you've given me
Code:
Private Sub On_Time_Delivery_Report_Click()
On Error GoTo Err_On_Time_Delivery_Report_Click

    Dim stDocName As String

    stDocName = "On-Time Delivery Report"
    DoCmd.OpenReport stDocName, acPreview
    DoCmd.Close acform, me.Name

Exit_On_Time_Delivery_Report_Click:
    Exit Sub

Err_On_Time_Delivery_Report_Click:
    MsgBox Err.Description
    Resume Exit_On_Time_Delivery_Report_Click
    
End Sub
 
That worked thank you, quick question for whomever, what would my condition be in an IF statement if I want to check for whether or not a certain column, called first_val, contains any values?
iow, I don't want to go into the IF statement if the column does not contain any values. Thank you, you guys are very helpful
 
Last edited:
so you would check
if not isnull(first_val)
 
so you would check
if not isnull(first_val)

Just in case - to handle empty strings as well, I would suggest using:
Code:
If Len([first_val] & "") > 0 Then
  '...do your stuff here
End If
 

Users who are viewing this thread

Back
Top Bottom