Beginner's explanation for API Dialog Box Code?

  • Thread starter Thread starter Chris C via AccessMonster.com
  • Start date Start date
C

Chris C via AccessMonster.com

All,

Can someone provide a step-by-step for us beginners on how to use the API
Dialog box code and where to place it, etc...

Basically what I think some of us would need it for is to create command
buttons for "open" and "save as"..

Can someone show us how to...

1. place the code properly and where to place it
2. how to call the functions correctly
3. where to put the code in the command buttons.

Thank you so much for your help in advance.

Chris
 
Here is an example for opening a file. This code does not actually open the
file, it only gets the file and path name. All the options involved are only
for the presentation of the dialog:

'Flags Hides the Read Only Check and Only allow existing files
lngFlags = ahtOFN_HIDEREADONLY Or ahtOFN_FILEMUSTEXIST
'Set filter to show only csv spreadsheets
strfilter = ahtAddFilterItem(strfilter, "Text Files (*.csv)")
'Set the Default File Name
strFileName = "VoughtBillCurrentMonth.csv"
'Set the Default Path
strDefaultDir = "\\rsltx1-bm01\FTPData$"
'Call the Open File Dialog
Do While True
varGetFileName = ahtCommonFileOpenSave( _
OpenFile:=True, _
Filter:=strfilter, _
FileName:=strFileName, _
InitialDir:=strDefaultDir, _
Flags:=lngFlags, _
DialogTitle:="Import BillCurrMonth3345")
Me.Repaint 'I put this in because I was having display problem
If varGetFileName = "" Then 'User clicked Cancel
If MsgBox("Cancel Import?", vbQuestion + vbYesNo, "Import
BillCurrMonth3345") _
= vbYes Then
blnOkGo = False
Exit Do
End If
Else
blnOkGo = True
Exit Do
End If
Loop

In this case, I am getting the file name so I can link to it:
'Link to the csv file
DoCmd.TransferText acLinkDelim, "VoughtBillCurrentMonth Link
Specification", _
"VoughtBillCurrentMonth", varGetFileName, True

In this example, I am getting the name of a file I want to copy a recordset
to:
'Get filename to copy to
'Flags Hides the Read Only Check
lngFlags = ahtOFN_HIDEREADONLY Or ahtOFN_OVERWRITEPROMPT
'Set filter to show only xls Spreadsheets
strfilter = ahtAddFilterItem(strfilter, "Excel Spreadsheet Files (*.xls)")
'Set the Default File Name
strFileName = "ImportErrors" & "_" & _
Forms!frmLoadMonth!cboPeriod.Column(1) & "_" & _
Forms!frmLoadMonth!txtCurrYear & ".csv"
'Set the Default Path
strDefaultDir = "\\rsltx1-bm01\busmgmt\Dev CISCMS\BillCurrMonthBackup\"
'Call the Open File Dialog
varGetSaveName = ahtCommonFileOpenSave( _
OpenFile:=False, _
Filter:=strfilter, _
FileName:=strFileName, _
InitialDir:=strDefaultDir, _
Flags:=lngFlags, _
DialogTitle:="Save Error File")
Me.Repaint
If varGetSaveName = "" Then
MsgBox "Save File Canceled", vbOKOnly, "Save Errors"
GoTo cmdSave_Click_Exit
End If

Now I have the file name in varGetSaveName, So I need to create the
spreadsheet and copy the data to it. Why not a TranferSpreadsheet, you ask?
Seems that in this case, because I have the table as the recordset of my
subform, I was getting "recordset is read only" error, so I got around it
this way. Why is would let me do this and not a TransferSpreadsheet, I don't
know.

On Error Resume Next ' Defer error trapping.
Set xlApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
blnExcelWasNotRunning = True
Set xlApp = CreateObject("excel.application")
Else
DetectExcel
End If
Err.Clear ' Clear Err object in case error occurred.
On Error GoTo cmdSave_Click_Error
DoEvents
xlApp.DisplayAlerts = False
'Create the Workbook
Set xlBook = xlApp.Workbooks.Add

Set xlSheet = xlBook.Worksheets(1)
For lngCtr = 0 To rst.Fields.Count - 1
xlSheet.Cells(1, lngCtr + 1) = rst.Fields(lngCtr).Name
Next lngCtr

xlSheet.Cells(2, 1).CopyFromRecordset rst
xlBook.SaveAs FileName:=varGetSaveName

All the variables and objects need to be dimmed.
Your API code should be in a standard module by itself.
The code examples above go in the Click Event of the command button you will
use, one for Save and one for Open.
Not the Do While True Loops. That is there so the user can make mistakes.
If they decide not to open or save the file, then you will get a zero length
("") string returned, and bypass the code that uses the variable.

If you have any questions on this, please post back. I hope this answers
your questions.
 
Klatuu,

Thanks for the response - I sort of understood what you posted, and I thank
you very much for doing so...

What I think I was looking for was an explanation of the API code that is
found at:

http://www.mvps.org/access/api/api0001.htm

this is the site that is posted all the time on here for this code.

I guess I am too new at this stuff, and the furthest I've gotten was making
this a public module in my DB - I'm looking to figure out how to do a "save
as" to excel for an export using the code on the website. Let me know if you
can assist. Thanks again for your help.

Chris
Here is an example for opening a file. This code does not actually open the
file, it only gets the file and path name. All the options involved are only
for the presentation of the dialog:

'Flags Hides the Read Only Check and Only allow existing files
lngFlags = ahtOFN_HIDEREADONLY Or ahtOFN_FILEMUSTEXIST
'Set filter to show only csv spreadsheets
strfilter = ahtAddFilterItem(strfilter, "Text Files (*.csv)")
'Set the Default File Name
strFileName = "VoughtBillCurrentMonth.csv"
'Set the Default Path
strDefaultDir = "\\rsltx1-bm01\FTPData$"
'Call the Open File Dialog
Do While True
varGetFileName = ahtCommonFileOpenSave( _
OpenFile:=True, _
Filter:=strfilter, _
FileName:=strFileName, _
InitialDir:=strDefaultDir, _
Flags:=lngFlags, _
DialogTitle:="Import BillCurrMonth3345")
Me.Repaint 'I put this in because I was having display problem
If varGetFileName = "" Then 'User clicked Cancel
If MsgBox("Cancel Import?", vbQuestion + vbYesNo, "Import
BillCurrMonth3345") _
= vbYes Then
blnOkGo = False
Exit Do
End If
Else
blnOkGo = True
Exit Do
End If
Loop

In this case, I am getting the file name so I can link to it:
'Link to the csv file
DoCmd.TransferText acLinkDelim, "VoughtBillCurrentMonth Link
Specification", _
"VoughtBillCurrentMonth", varGetFileName, True

In this example, I am getting the name of a file I want to copy a recordset
to:
'Get filename to copy to
'Flags Hides the Read Only Check
lngFlags = ahtOFN_HIDEREADONLY Or ahtOFN_OVERWRITEPROMPT
'Set filter to show only xls Spreadsheets
strfilter = ahtAddFilterItem(strfilter, "Excel Spreadsheet Files (*.xls)")
'Set the Default File Name
strFileName = "ImportErrors" & "_" & _
Forms!frmLoadMonth!cboPeriod.Column(1) & "_" & _
Forms!frmLoadMonth!txtCurrYear & ".csv"
'Set the Default Path
strDefaultDir = "\\rsltx1-bm01\busmgmt\Dev CISCMS\BillCurrMonthBackup\"
'Call the Open File Dialog
varGetSaveName = ahtCommonFileOpenSave( _
OpenFile:=False, _
Filter:=strfilter, _
FileName:=strFileName, _
InitialDir:=strDefaultDir, _
Flags:=lngFlags, _
DialogTitle:="Save Error File")
Me.Repaint
If varGetSaveName = "" Then
MsgBox "Save File Canceled", vbOKOnly, "Save Errors"
GoTo cmdSave_Click_Exit
End If

Now I have the file name in varGetSaveName, So I need to create the
spreadsheet and copy the data to it. Why not a TranferSpreadsheet, you ask?
Seems that in this case, because I have the table as the recordset of my
subform, I was getting "recordset is read only" error, so I got around it
this way. Why is would let me do this and not a TransferSpreadsheet, I don't
know.

On Error Resume Next ' Defer error trapping.
Set xlApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
blnExcelWasNotRunning = True
Set xlApp = CreateObject("excel.application")
Else
DetectExcel
End If
Err.Clear ' Clear Err object in case error occurred.
On Error GoTo cmdSave_Click_Error
DoEvents
xlApp.DisplayAlerts = False
'Create the Workbook
Set xlBook = xlApp.Workbooks.Add

Set xlSheet = xlBook.Worksheets(1)
For lngCtr = 0 To rst.Fields.Count - 1
xlSheet.Cells(1, lngCtr + 1) = rst.Fields(lngCtr).Name
Next lngCtr

xlSheet.Cells(2, 1).CopyFromRecordset rst
xlBook.SaveAs FileName:=varGetSaveName

All the variables and objects need to be dimmed.
Your API code should be in a standard module by itself.
The code examples above go in the Click Event of the command button you will
use, one for Save and one for Open.
Not the Do While True Loops. That is there so the user can make mistakes.
If they decide not to open or save the file, then you will get a zero length
("") string returned, and bypass the code that uses the variable.

If you have any questions on this, please post back. I hope this answers
your questions.
[quoted text clipped - 13 lines]
 
The code I posted uses the API to which you refer. It will probably take
some experimenting for you to get your head around it. As to save as for an
export, it depends on whether you are doing a TransferSpreadsheet or saving
an open spreadsheet you have modified or created. For the
TranferSpreadsheet, the varilabe varGetSaveName would be used for the file
name argument:
DoCmd.TransferSpreadsheet acImport, 3, _
"Employees",varSaveFileName, True, "A1:G12"

If you are using an Excel Object, then it would be as in my previous post
using the SaveAs method:

xlBook.SaveAs FileName:=varGetSaveName

If you have questions about any specific lines in the code, post back and I
will glad to help clear it up for your.
Chris C via AccessMonster.com said:
Klatuu,

Thanks for the response - I sort of understood what you posted, and I thank
you very much for doing so...

What I think I was looking for was an explanation of the API code that is
found at:

http://www.mvps.org/access/api/api0001.htm

this is the site that is posted all the time on here for this code.

I guess I am too new at this stuff, and the furthest I've gotten was making
this a public module in my DB - I'm looking to figure out how to do a "save
as" to excel for an export using the code on the website. Let me know if you
can assist. Thanks again for your help.

Chris
Here is an example for opening a file. This code does not actually open the
file, it only gets the file and path name. All the options involved are only
for the presentation of the dialog:

'Flags Hides the Read Only Check and Only allow existing files
lngFlags = ahtOFN_HIDEREADONLY Or ahtOFN_FILEMUSTEXIST
'Set filter to show only csv spreadsheets
strfilter = ahtAddFilterItem(strfilter, "Text Files (*.csv)")
'Set the Default File Name
strFileName = "VoughtBillCurrentMonth.csv"
'Set the Default Path
strDefaultDir = "\\rsltx1-bm01\FTPData$"
'Call the Open File Dialog
Do While True
varGetFileName = ahtCommonFileOpenSave( _
OpenFile:=True, _
Filter:=strfilter, _
FileName:=strFileName, _
InitialDir:=strDefaultDir, _
Flags:=lngFlags, _
DialogTitle:="Import BillCurrMonth3345")
Me.Repaint 'I put this in because I was having display problem
If varGetFileName = "" Then 'User clicked Cancel
If MsgBox("Cancel Import?", vbQuestion + vbYesNo, "Import
BillCurrMonth3345") _
= vbYes Then
blnOkGo = False
Exit Do
End If
Else
blnOkGo = True
Exit Do
End If
Loop

In this case, I am getting the file name so I can link to it:
'Link to the csv file
DoCmd.TransferText acLinkDelim, "VoughtBillCurrentMonth Link
Specification", _
"VoughtBillCurrentMonth", varGetFileName, True

In this example, I am getting the name of a file I want to copy a recordset
to:
'Get filename to copy to
'Flags Hides the Read Only Check
lngFlags = ahtOFN_HIDEREADONLY Or ahtOFN_OVERWRITEPROMPT
'Set filter to show only xls Spreadsheets
strfilter = ahtAddFilterItem(strfilter, "Excel Spreadsheet Files (*.xls)")
'Set the Default File Name
strFileName = "ImportErrors" & "_" & _
Forms!frmLoadMonth!cboPeriod.Column(1) & "_" & _
Forms!frmLoadMonth!txtCurrYear & ".csv"
'Set the Default Path
strDefaultDir = "\\rsltx1-bm01\busmgmt\Dev CISCMS\BillCurrMonthBackup\"
'Call the Open File Dialog
varGetSaveName = ahtCommonFileOpenSave( _
OpenFile:=False, _
Filter:=strfilter, _
FileName:=strFileName, _
InitialDir:=strDefaultDir, _
Flags:=lngFlags, _
DialogTitle:="Save Error File")
Me.Repaint
If varGetSaveName = "" Then
MsgBox "Save File Canceled", vbOKOnly, "Save Errors"
GoTo cmdSave_Click_Exit
End If

Now I have the file name in varGetSaveName, So I need to create the
spreadsheet and copy the data to it. Why not a TranferSpreadsheet, you ask?
Seems that in this case, because I have the table as the recordset of my
subform, I was getting "recordset is read only" error, so I got around it
this way. Why is would let me do this and not a TransferSpreadsheet, I don't
know.

On Error Resume Next ' Defer error trapping.
Set xlApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
blnExcelWasNotRunning = True
Set xlApp = CreateObject("excel.application")
Else
DetectExcel
End If
Err.Clear ' Clear Err object in case error occurred.
On Error GoTo cmdSave_Click_Error
DoEvents
xlApp.DisplayAlerts = False
'Create the Workbook
Set xlBook = xlApp.Workbooks.Add

Set xlSheet = xlBook.Worksheets(1)
For lngCtr = 0 To rst.Fields.Count - 1
xlSheet.Cells(1, lngCtr + 1) = rst.Fields(lngCtr).Name
Next lngCtr

xlSheet.Cells(2, 1).CopyFromRecordset rst
xlBook.SaveAs FileName:=varGetSaveName

All the variables and objects need to be dimmed.
Your API code should be in a standard module by itself.
The code examples above go in the Click Event of the command button you will
use, one for Save and one for Open.
Not the Do While True Loops. That is there so the user can make mistakes.
If they decide not to open or save the file, then you will get a zero length
("") string returned, and bypass the code that uses the variable.

If you have any questions on this, please post back. I hope this answers
your questions.
[quoted text clipped - 13 lines]
 
Let me start with step 1... I am using the code from the link above and
placing it into a public module. Then I am supposed to take code and place
it into the click event - what code is this and where do I build it from? Do
I have to construct it from the module code? I'm understanding slowly buy
surely.
The code I posted uses the API to which you refer. It will probably take
some experimenting for you to get your head around it. As to save as for an
export, it depends on whether you are doing a TransferSpreadsheet or saving
an open spreadsheet you have modified or created. For the
TranferSpreadsheet, the varilabe varGetSaveName would be used for the file
name argument:
DoCmd.TransferSpreadsheet acImport, 3, _
"Employees",varSaveFileName, True, "A1:G12"

If you are using an Excel Object, then it would be as in my previous post
using the SaveAs method:

xlBook.SaveAs FileName:=varGetSaveName

If you have questions about any specific lines in the code, post back and I
will glad to help clear it up for your.
[quoted text clipped - 129 lines]
 
The code you downloaded goes in a standard module all by itself. There is an
example in the code on how to call it, but ignore that.

The code examples I posted go in the click events of their respective
command buttons. It sets up the options available in the API code and calls
that code.

varGetSaveName = ahtCommonFileOpenSave(...
is the entry point for using the API.

The GetOpenFile function in the API module is an example of how to use it.
The example is not very inclusive. The examples I sent are more robust.

Chris C via AccessMonster.com said:
Let me start with step 1... I am using the code from the link above and
placing it into a public module. Then I am supposed to take code and place
it into the click event - what code is this and where do I build it from? Do
I have to construct it from the module code? I'm understanding slowly buy
surely.
The code I posted uses the API to which you refer. It will probably take
some experimenting for you to get your head around it. As to save as for an
export, it depends on whether you are doing a TransferSpreadsheet or saving
an open spreadsheet you have modified or created. For the
TranferSpreadsheet, the varilabe varGetSaveName would be used for the file
name argument:
DoCmd.TransferSpreadsheet acImport, 3, _
"Employees",varSaveFileName, True, "A1:G12"

If you are using an Excel Object, then it would be as in my previous post
using the SaveAs method:

xlBook.SaveAs FileName:=varGetSaveName

If you have questions about any specific lines in the code, post back and I
will glad to help clear it up for your.
[quoted text clipped - 129 lines]
 
Thanks so much - let me wrap myself around this tonight and tomorrow and I
will be back, I promise - thanks so much for your assistance. Mastering this
would be a big help for my department.

chris
The code you downloaded goes in a standard module all by itself. There is an
example in the code on how to call it, but ignore that.

The code examples I posted go in the click events of their respective
command buttons. It sets up the options available in the API code and calls
that code.

varGetSaveName = ahtCommonFileOpenSave(...
is the entry point for using the API.

The GetOpenFile function in the API module is an example of how to use it.
The example is not very inclusive. The examples I sent are more robust.
Let me start with step 1... I am using the code from the link above and
placing it into a public module. Then I am supposed to take code and place
[quoted text clipped - 24 lines]
 
You will get there, Chris. Once you have it down, it is a very useful tool.
In my world, we do a lot of importing from, exporting to, and creating new
spreadsheets. It is one of the most useful tools in my bag.

Chris C via AccessMonster.com said:
Thanks so much - let me wrap myself around this tonight and tomorrow and I
will be back, I promise - thanks so much for your assistance. Mastering this
would be a big help for my department.

chris
The code you downloaded goes in a standard module all by itself. There is an
example in the code on how to call it, but ignore that.

The code examples I posted go in the click events of their respective
command buttons. It sets up the options available in the API code and calls
that code.

varGetSaveName = ahtCommonFileOpenSave(...
is the entry point for using the API.

The GetOpenFile function in the API module is an example of how to use it.
The example is not very inclusive. The examples I sent are more robust.
Let me start with step 1... I am using the code from the link above and
placing it into a public module. Then I am supposed to take code and place
[quoted text clipped - 24 lines]
 
Back
Top