Solved

error on vba access function, ByRef

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

758 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

22 Experts available now in Live!

Get 1:1 Help Now