Macro Permission denied

I'm trying to run a function using a Macro. When I try to run the macro, I receive a Run-time error "70" Permission denied.
'

The error occurs at line :Open FileNameAndPath For Output Access Write Lock Write As FileNum


The Marco is basically is run code:

Function Name: Export_Tab_Delimited ( "tblAGRSalesProjection", "F:\Supply Chain\Team Folders\Metrics\AGR Sales Projection\tblAGRSalesProjection.txt")



Option Compare Database
Option Explicit
Public Function Export_Tab_Delimited(TableOrQueryName As String, FileNameAndPath As String)
'This uses the DAO database reference. IN the VB window (Ctrl+G) go to _
Tools --> References and select the Microsoft DAO 3.6 Library
  
Dim DB As DAO.Database
Dim RS As DAO.Recordset
Dim I As Integer
Dim FileNum As Integer
'Dim FileNameAndPath As String
Dim OutputLine As String
FileNum = FreeFile()
'FileNameAndPath = "F:\Supply Chain\Team Folders\Metrics\AGR Sales Projection\tblAGRSalesProjection1.txt"
Set DB = CurrentDb()                              'Use the current database
Set RS = DB.OpenRecordset("qryAGRSalesProjection")      'actually open the recordset
If RS.EOF = False Then
RS.MoveFirst
Else
MsgBox "No Data", vbExclamation, "Exiting Fuction"
Set RS = Nothing
Set DB = Nothing
Exit Function
End If
'Open the file for output
Open FileNameAndPath For Output Access Write Lock Write As FileNum
I = 0
OutputLine = ""
'Output the field names as a header
For I = 0 To RS.Fields.Count - 1
If I > 0 Then
OutputLine = OutputLine & Chr(9) & RS.Fields(I).Name
Else
OutputLine = RS.Fields(I).Name
End If
Next I
Print #FileNum, OutputLine
Debug.Print OutputLine
I = 0
OutputLine = ""
'start outputting the data
Do Until RS.EOF
For I = 0 To RS.Fields.Count - 1
If I > 0 Then
OutputLine = OutputLine & Chr(9) & RS.Fields(I).Value
Else
OutputLine = RS.Fields(I).Value
End If
Next I
Print #FileNum, OutputLine
Debug.Print OutputLine
OutputLine = ""
RS.MoveNext
Loop
Close #FileNum
Set RS = Nothing
Set DB = Nothing
End Function

Open in new window

Buckeye4LifeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

therealmongooseCommented:
Your code looks ok, I'd suggest you try to changing

Open FileNameAndPath For Output Access Write Lock Write As FileNum

to

Open FileNameAndPath For append As FileNum

If this doesn't work it may either be a permissions error on you server (try a temp file on the c drive instead to see if the code works) If it is permissions you need to check write permissions for your user name.

The other possibility is that your original code locks the file for output to - if you've crashed a procedure when you have been testing, you may still havea locked file on the server - try calling

 ( "tblAGRSalesProjection", "F:\Supply Chain\Team Folders\Metrics\AGR Sales Projection\tblAGRSalesProjection_Test.txt")

"F:\Supply Chain\Team Folders\Metrics\AGR Sales Projection\tblAGRSalesProjection.txt"

Is probably locked which is why your code isn't working....

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Buckeye4LifeAuthor Commented:
I found out that my boss had the txt file open on the server. Thanks for you're input, it was helpful.
0
therealmongooseCommented:
Lol - no probs, glad it helped - (bosses - always trying to be helpful... tssk!!)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.