Solved

error on vba access function, ByRef

Posted on 2016-10-03
4
51 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 39

Accepted Solution

by:
als315 earned 250 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 19

Assisted Solution

by:crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access earned 250 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 19
ID: 41840111
you're welcome ~ happy to help
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now