Link to home
Create AccountLog in
Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.ScFlag for Zambia

asked on

Error in saving the Ms Access text file

First Many thanks to you all for trying very to assist on how to save string from MS Access to a text file, however, there are still a few things remaining to get it right, the file to save is giving a debug error when trying save. Could it be I'm going off the line again?

Private Sub CmdServings_Click()
Dim strData As String
Dim n As Integer
Dim dlg As filedialog
Dim GetFilePath As Object
    Set dlg = Application.filedialog(msoFileDialogSaveAs)
    dlg.AllowMultiSelect = False
    dlg.Title = "Select the file to be imported..."
   
    If dlg.Show Then
    GetFilePath = dlg.SelectedItems(1)
    End If
n = FreeFile()
Print #n, strData
Close #n
End Sub

Open in new window



Avatar of Kimputer
Kimputer

Should work by adding only one line:

Private Sub CmdServings_Click()
Dim strData As String
Dim n As Integer
Dim dlg As filedialog
Dim GetFilePath As Object
    Set dlg = Application.filedialog(msoFileDialogSaveAs)
    dlg.AllowMultiSelect = False
    dlg.Title = "Select the file to be imported..."
   
    If dlg.Show Then
    GetFilePath = dlg.SelectedItems(1)
    End If
n = FreeFile()
Open GetFilePath For Output As #n
Print #n, strData
Close #n
End Sub

Open in new window

Though some other errors will follow next, GetFilePath should be string, strData is empty etc etc
Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc

ASKER

User generated image
I have also tried to load strData with some manual data so that it does not show empty but still the debug error pops up.

Private Sub CmdServings_Click()
Dim strData As String
Dim n As Integer
Dim dlg As filedialog
Dim GetFilePath As Object
strData = "145236tyyiio"
    Set dlg = Application.filedialog(msoFileDialogSaveAs)
    dlg.AllowMultiSelect = False
    dlg.Title = "Select the file to be export..."
   
    If dlg.Show Then
    GetFilePath = dlg.SelectedItems(1)
    End If
n = FreeFile()
Open GetFilePath For Output As #n
Print #n, strData
Close #n
End Sub

Open in new window

Here are the series of errors I'm getting:



User generated image


User generated image
ASKER CERTIFIED SOLUTION
Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Flag of Zambia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Did you cancel the process.

You should use a file picking function and simply use the returned value.  This simplifies your code, allow reuse, make your code much more dependable.  I urge you to simply use a function like https://www.devhut.net/2016/10/04/late-binding-the-filedialog/
Also thanks for marking your own answer, while my FIRST POST said:

GetFilePath should be string