Solved

error on vba access function, ByRef

Posted on 2016-10-03
4
84 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

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 21

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 21
ID: 41840111
you're welcome ~ happy to help
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

717 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