Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Flag 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



Microsoft Access

Avatar of undefined
Last Comment
Kimputer

8/22/2022 - Mon
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
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc

ASKER

Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc

ASKER
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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc

ASKER
Here are the series of errors I'm getting:






ASKER CERTIFIED SOLUTION
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Daniel Pineault

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/
Kimputer

Also thanks for marking your own answer, while my FIRST POST said:

GetFilePath should be string
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.