Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

error on vba access function, ByRef

Posted on 2016-10-03
4
Medium Priority
?
109 Views
Last Modified: 2016-10-12
Hello Guys,

I have two function, the first function ask the user to informa a file.txt and then pass the file to the second function that have to open the file.
But access is complaining about it with " incompatible argument byRef"

Please, take a look at my code:

Private Sub Comando0_Click()
  Dim Message, Title, Default, MyValue
  
  If MsgBox("Deseja realmente rodar o script??", vbQuestion + vbYesNo, "Script") = vbYes Then
     Message = "Informe o script"       ' Set prompt.
     Title = "Execução de Script SQL"   ' Set title.
     Default = ""                       ' Set default.
     MyValue = InputBox(Message, Title, Default)
     
     If Trim(MyValue) = "" Then
        MsgBox "Script não informado, será pego o padrão da pasta!", vbOKOnly, "Execução de Script"
     Else
        If Dir(strSourceFile) = vbNullString Then
           MsgBox "Não existe o arquivo de script na pasta padão!", vbOKOnly
        Else
          Call ReadSql(MyValue)
        End If
        
     End If
  End If
End Sub


Public Function ReadSql(arq As String)
   
    Dim intFileDesc As Integer          'File descriptor for output file
    Dim strSourceFile As String         'Full path of source file
    Dim strTextLine As String           'Input buffer


    Set dbs = CurrentDb
    
    If arq = "" Then
       strSourceFile = "c:\access\script.sql"
    Else
       strSourceFile = arq
    End If
    
    If Dir(strSourceFile) = vbNullString Then
       MsgBox "Não existe o arquivo de script na pasta padão!", vbOKOnly
    End If
    
    intFileDesc = FreeFile
    Open strSourceFile For Input As #intFileDesc
    Do While Not EOF(intFileDesc)               ' Loop until end of file.
        Line Input #intFileDesc, strTextLine    ' Read line into buffer
        CurrentDb.Execute strTextLine
    Loop
    Close #intFileDesc    'Close file.
    
    
    Set qdf = Nothing
    Set dbs = Nothing     'Garbage handling before we exit the function

End Function

Open in new window


Why? How to solve that?
Thanks
Alex
0
Comment
Question by:hidrau
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
als315 earned 1000 total points
ID: 41827077
You should always declare type of variable. Add "Option explicit" to any module:
Option Explicit
Private Sub Comando0_Click()
  Dim Message As String, Title As String, Default As String, MyValue As String
  
  If MsgBox("Deseja realmente rodar o script??", vbQuestion + vbYesNo, "Script") = vbYes Then
     Message = "Informe o script"       ' Set prompt.
     Title = "Execução de Script SQL"   ' Set title.
     Default = ""                       ' Set default.
     MyValue = InputBox(Message, Title, Default)
     
     If Trim(MyValue) = "" Then
        MsgBox "Script não informado, será pego o padrão da pasta!", vbOKOnly, "Execução de Script"
     Else
        If Dir(MyValue) = vbNullString Then
           MsgBox "Não existe o arquivo de script na pasta padão!", vbOKOnly
        Else
          Call ReadSql(MyValue)
        End If
        
     End If
  End If
End Sub


Public Function ReadSql(arq As String)
   
    Dim intFileDesc As Integer          'File descriptor for output file
    Dim strSourceFile As String         'Full path of source file
    Dim strTextLine As String           'Input buffer
    Dim dbs As DAO.Database
    'Dim qdf As DAO.Recordset
    Set dbs = CurrentDb
    
    If arq = "" Then
       strSourceFile = "c:\access\script.sql"
    Else
       strSourceFile = arq
    End If
    
    If Dir(strSourceFile) = vbNullString Then
       MsgBox "Não existe o arquivo de script na pasta padão!", vbOKOnly
    End If
    
    intFileDesc = FreeFile
    Open strSourceFile For Input As #intFileDesc
    Do While Not EOF(intFileDesc)               ' Loop until end of file.
        Line Input #intFileDesc, strTextLine    ' Read line into buffer
        CurrentDb.Execute strTextLine
    Loop
    Close #intFileDesc    'Close file.
    
    
    'Set qdf = Nothing
    Set dbs = Nothing     'Garbage handling before we exit the function

End Function

Open in new window

1
 
LVL 23

Assisted Solution

by:crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access earned 1000 total points
ID: 41827367
it is complaining because MyValue is a variant (since you did not explicitly define it to be something else), not a string (even though you are assigning a string value)
if you want to keep it as a variant, instead of arq As String in the declaration for ReadSql, use:  
   ByVal arq As variant

Open in new window

or if you dimension it to be a string, then you can keep As String.  You might still want to use ByVal.  This means a copy of the value will be made in the called procedure and the variable that was sent won't be changed if the called procedure changes it.

You should explicitly declare the data type for every variable, even if it will be a variant (the default if you do not specify something). Also, as recommended by als315, Option Explicit is a good habit.  That helps the compiler catch more mistakes and enables Intellisense to kick in more as you are typing.
1
 
LVL 1

Author Closing Comment

by:hidrau
ID: 41840101
thanks a lot
0
 
LVL 23
ID: 41840111
you're welcome ~ happy to help
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question