Results 1 to 2 of 2
  1. #1
    eww is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2010
    Posts
    125

    Save As Dialog Box for Reports

    I'm trying to get this to work but I'm running into an error

    What I'm currently using is mainly this:
    http://bytes.com/topic/access/answer...ame-dialog-box

    which I am pretty certain comes from here:
    http://msdn.microsoft.com/en-us/libr...ffice.10).aspx

    I've cleaned things up a bit more (I was getting some debugging errors because of the way things were laid out) but I'm still getting an error and I'm not sure what it means.

    Here is my code, the pink bold part is where I'm getting the error "Argument not optional"

    Class Module (CommonDialogAPI)
    Code:
    Option Compare Database
    Option Explicit
    'Code needed to use the Windows "Open File" and "Save As" Dialog Boxes
    'to capture file path for exporting spreadsheets, etc.
     
    'Declare needed functions
    Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" _
        (pOpenfilename As OPENFILENAME) As Long
     
    Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" _
        (pOpenfilename As OPENFILENAME) As Long
     
    'Declare OPENFILENAME custom Type
    Private Type OPENFILENAME
        lStructSize As Long
        hwndOwner As Long
        hInstance As Long
        lpstrFilter As String
        lpstrCustomFilter As String
        nMaxCustFilter As Long
        nFilterIndex As Long
        lpstrFile As String
        nMaxFile As Long
        lpstrFileTitle As String
        nMaxFileTitle As Long
        lpstrInitialDir As String
        lpstrTitle As String
        Flags As Long
        nFileOffset As Integer
        nFileExtension As Integer
        lpstrDefExt As String
        lCustData As Long
        lpfnHook As Long
        lpTemplateName As String
    End Type
     
    Private mstrFileName As String
    Private mblnStatus As Boolean
     
     
    Public Property Let GetName(strName As String)
        mstrFileName = strName
    End Property
     
    Public Property Get GetName() As String
        GetName = mstrFileName
    End Property
     
    Public Property Let GetStatus(blnStatus As Boolean)
        mblnStatus = blnStatus
    End Property
     
    Public Property Get GetStatus() As Boolean
        GetStatus = mblnStatus
    End Property
     
    'Function needed to call the "Open File" dialog
    Public Function OpenFileDialog(lngFormHwnd As Long, _
                                   lngAppInstance As Long, _
                                   strInitDir As String, _
                                   strFileFilter As String) As Long
        Dim OpenFile As OPENFILENAME
        Dim X As Long
     
        With OpenFile
            .lStructSize = Len(OpenFile)
            .hwndOwner = lngFormHwnd
            .hInstance = lngAppInstance
            .lpstrFilter = strFileFilter
            .nFilterIndex = 1
            .lpstrFile = String(257, 0)
            .nMaxFile = Len(OpenFile.lpstrFile) - 1
            .lpstrFileTitle = OpenFile.lpstrFile
            .nMaxFileTitle = OpenFile.nMaxFile
            .lpstrInitialDir = strInitDir
            .lpstrTitle = "Open File"
            .Flags = 0 'warns the user before overwriting an existing file
        End With
     
        X = GetOpenFileName(OpenFile)
        If X = 0 Then
            mstrFileName = "none"
            mblnStatus = False
        Else
            mstrFileName = Trim(OpenFile.lpstrFile)
            mblnStatus = True
        End If
    End Function
     
    'Function needed to call the "Save As" dialog
    Public Function SaveFileDialog(lngFormHwnd As Long, _
                                   lngAppInstance As Long, _
                                   strInitDir As String, _
                                   strFileFilter As String, _
                                   strFileName As String) As Long
        Dim SaveFile As OPENFILENAME
        Dim X As Long
     
        If IsMissing(strFileName) Then strFileName = ""
     
        With SaveFile
            .lStructSize = Len(SaveFile)
            .hwndOwner = lngFormHwnd
            .hInstance = lngAppInstance
            .lpstrFilter = strFileFilter
            .nFilterIndex = 1
            .lpstrFile = "testfile.xls" & String(257 - Len("testfile.xls"), 0)
            .nMaxFile = Len(SaveFile.lpstrFile) - 1
            .lpstrFileTitle = SaveFile.lpstrFile
            .nMaxFileTitle = SaveFile.nMaxFile
            .lpstrInitialDir = strInitDir
            .lpstrTitle = "Export To"
            .Flags = 0 'warns the user before overwriting an existing file
            .lpstrDefExt = ".xls"   'sets default file extension to Excel, in case user does not type it
        End With
     
        X = GetSaveFileName(SaveFile)
        If X = 0 Then
            mstrFileName = "none"
            mblnStatus = False
        Else
            mstrFileName = Trim(SaveFile.lpstrFile)
            mblnStatus = True
        End If
    End Function
    Actual Command To Run It (From a Button On a Report)


    Code:
    Option Compare Database
     
    Private Sub Command55_Click()
        Dim cDlg As New CommonDialogAPI     'instantiate CommonDialog
        Dim lngFormHwnd As Long
        Dim lngAppInstance As Long
        Dim strInitDir As String
        Dim strFileFilter As String
        Dim lngResult As Long
     
        lngFormHwnd = Me.Hwnd
        lngAppInstance = Application.hWndAccessApp
        strInitDir = "C:\"
     
        strFileFilter = "Excel Files (*.xls)" & _
            Chr(0) & "*.xls" & Chr(0) & _
            "Text Files (*.csv, *.txt)" & _
            Chr(0) & "*.csv; *.txt" & Chr(0)
     
        lngResult = cDlg.SaveFileDialog(lngFormHwnd, _
            lngAppInstance, strInitDir, strFileFilter)
     
        If cDlg.GetStatus = True Then
            MsgBox "You selected file: " & cDlg.GetName
            strSavePath = cDlg.GetName  'assign selected path to variable to be passed to TransferSpreadsheet Method
        Else
            MsgBox "No file selected."
        End If
    End Sub

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    you have the wrong number of arguments in the function where you've bolded the text. That's the reason why. It requires 5 arguments, and all of them must be included.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Dialog box form example
    By pkstormy in forum Code Repository
    Replies: 0
    Last Post: 08-30-2010, 10:45 PM
  2. Play sounds and Common Dialog Box example
    By pkstormy in forum Code Repository
    Replies: 0
    Last Post: 08-29-2010, 06:49 AM
  3. Pop-up Dialog Box
    By DianeG in forum Reports
    Replies: 4
    Last Post: 03-29-2010, 09:51 AM
  4. Unwanted Dialog Box
    By ntallman in forum Programming
    Replies: 3
    Last Post: 03-26-2010, 11:11 AM
  5. dialog box errors in report
    By Jane in forum Programming
    Replies: 0
    Last Post: 11-20-2008, 01:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums