One command button for 2 events?

Z

Zoe

I have a form that has 2 command buttons.

This one runs a Macro:
Private Sub impbuild_Click()
On Error GoTo Err_impbuild_Click
Dim stDocName As String
stDocName = "Build Export"
DoCmd.RunMacro stDocName
Exit_impbuild_Click:
Exit Sub
Err_impbuild_Click:
MsgBox Err.Description
Resume Exit_impbuild_Click
End Sub

This one exports a text file:
Private Sub exp_Click()
DoCmd.TransferText acExportDelim, "Export Reconciliation Export
Specification", "Export Reconciliation", [Forms]![Menu]![Path] & "\" & "BOS"
& Format([Forms]![Menu]![bosdate], "mmddyyyy") & "IMPIMPORT" & ".txt"
End Sub

I would like to use one command button to do both. The Macro must run
first, before the file export.

Is this possible?

Thanks,
Zoe
 
R

Rick Brandt

Zoe said:
I have a form that has 2 command buttons.

This one runs a Macro:
Private Sub impbuild_Click()
On Error GoTo Err_impbuild_Click
Dim stDocName As String
stDocName = "Build Export"
DoCmd.RunMacro stDocName
Exit_impbuild_Click:
Exit Sub
Err_impbuild_Click:
MsgBox Err.Description
Resume Exit_impbuild_Click
End Sub

This one exports a text file:
Private Sub exp_Click()
DoCmd.TransferText acExportDelim, "Export Reconciliation Export
Specification", "Export Reconciliation", [Forms]![Menu]![Path] & "\"
& "BOS" & Format([Forms]![Menu]![bosdate], "mmddyyyy") & "IMPIMPORT"
& ".txt"
End Sub

I would like to use one command button to do both. The Macro must run
first, before the file export.

Is this possible?

Thanks,
Zoe

Copy all of the lines between "Private Sub exp_Click()" and "End Sub" and
paste them before the "End Sub" of the other code.
 
Z

Zoe

Hi Rick,

I beleive that I followed your instructions. The macro runs but the file
export does not. The code looks like this now:

Private Sub impbuild_Click()
On Error GoTo Err_impbuild_Click

Dim stDocName As String

stDocName = "Build Export"
DoCmd.RunMacro stDocName

Exit_impbuild_Click:
Exit Sub

Err_impbuild_Click:
MsgBox Err.Description
Resume Exit_impbuild_Click

DoCmd.TransferText acExportDelim, "Export Reconciliation Export
Specification", "Export Reconciliation", [Forms]![Menu]![Path] & "\" & "BOS"
& Format([Forms]![Menu]![bosdate], "mmddyyyy") & "IMPIMPORT" & ".txt"

End Sub

Rick Brandt said:
Zoe said:
I have a form that has 2 command buttons.

This one runs a Macro:
Private Sub impbuild_Click()
On Error GoTo Err_impbuild_Click
Dim stDocName As String
stDocName = "Build Export"
DoCmd.RunMacro stDocName
Exit_impbuild_Click:
Exit Sub
Err_impbuild_Click:
MsgBox Err.Description
Resume Exit_impbuild_Click
End Sub

This one exports a text file:
Private Sub exp_Click()
DoCmd.TransferText acExportDelim, "Export Reconciliation Export
Specification", "Export Reconciliation", [Forms]![Menu]![Path] & "\"
& "BOS" & Format([Forms]![Menu]![bosdate], "mmddyyyy") & "IMPIMPORT"
& ".txt"
End Sub

I would like to use one command button to do both. The Macro must run
first, before the file export.

Is this possible?

Thanks,
Zoe

Copy all of the lines between "Private Sub exp_Click()" and "End Sub" and
paste them before the "End Sub" of the other code.
 
J

John W. Vinson

I beleive that I followed your instructions. The macro runs but the file
export does not. The code looks like this now:

Your "Exit Sub" line is firing and exiting prematurely. Move the error
handling to the end of the sub:

Private Sub impbuild_Click()
On Error GoTo Err_impbuild_Click

Dim stDocName As String

stDocName = "Build Export"
DoCmd.RunMacro stDocName

DoCmd.TransferText acExportDelim, _
"Export Reconciliation Export Specification", _
"Export Reconciliation", _
[Forms]![Menu]![Path] & "\BOS" & _
Format([Forms]![Menu]![bosdate], "mmddyyyy") & "IMPIMPORT.txt"

Exit_impbuild_Click:
Exit Sub

Err_impbuild_Click:
MsgBox Err.Description
Resume Exit_impbuild_Click


End Sub


I cleaned up the TransferText line by using the continuation characters " _"
and simplifying some of your text concatenations.
 
Z

Zoe

Thanks John and Rick! It works perfectly.
Zoe

John W. Vinson said:
I beleive that I followed your instructions. The macro runs but the file
export does not. The code looks like this now:

Your "Exit Sub" line is firing and exiting prematurely. Move the error
handling to the end of the sub:

Private Sub impbuild_Click()
On Error GoTo Err_impbuild_Click

Dim stDocName As String

stDocName = "Build Export"
DoCmd.RunMacro stDocName

DoCmd.TransferText acExportDelim, _
"Export Reconciliation Export Specification", _
"Export Reconciliation", _
[Forms]![Menu]![Path] & "\BOS" & _
Format([Forms]![Menu]![bosdate], "mmddyyyy") & "IMPIMPORT.txt"

Exit_impbuild_Click:
Exit Sub

Err_impbuild_Click:
MsgBox Err.Description
Resume Exit_impbuild_Click


End Sub


I cleaned up the TransferText line by using the continuation characters " _"
and simplifying some of your text concatenations.
 
Z

Zoe

One more question:

Is it possible to add some code that would alert the user if there isn't any
data and then not create the text file?

John W. Vinson said:
I beleive that I followed your instructions. The macro runs but the file
export does not. The code looks like this now:

Your "Exit Sub" line is firing and exiting prematurely. Move the error
handling to the end of the sub:

Private Sub impbuild_Click()
On Error GoTo Err_impbuild_Click

Dim stDocName As String

stDocName = "Build Export"
DoCmd.RunMacro stDocName

DoCmd.TransferText acExportDelim, _
"Export Reconciliation Export Specification", _
"Export Reconciliation", _
[Forms]![Menu]![Path] & "\BOS" & _
Format([Forms]![Menu]![bosdate], "mmddyyyy") & "IMPIMPORT.txt"

Exit_impbuild_Click:
Exit Sub

Err_impbuild_Click:
MsgBox Err.Description
Resume Exit_impbuild_Click


End Sub


I cleaned up the TransferText line by using the continuation characters " _"
and simplifying some of your text concatenations.
 

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