Solved Returning a value from the Funtion (1 Viewer)

donsi

Registered User.
Local time
Today, 13:58
Joined
Sep 1, 2016
Messages
73
Hi all,

I am trying to upload excel file and then run bunch of queries to update few tables based on uploaded file. I want to have a sub that calls function(to upload excel) with two vars supplied, and function in turn returns Boolean value back to sub. So the sub can use the boolean value to call or not call other sub which runs queries. I am having an issue writing this. Below is what I so far and it is causing compile error at If(updateUsers.importExcel....)

Sub Calling a function and passing file name and table name

Code:
Private Sub BtnImport_Click()
    Dim fso As New FileSystemObject
    If Nz(Me.TxtFileName, "") = "" Then 'Checking file path is entered
        MsgBox "Please select the file!"
        Exit Sub
    End If
    If fso.FileExists(Me.TxtFileName) Then
        If (UpdateUsers.ImportExcel Me.TxtFileName, "ActiveList" ) = True then 'Calling funtion to upload AL to a table
        Call Sync  ' Running queries to upload ActiveList
        End If
    Else
        MsgBox "File Not Found", vbInformation, "Incorrect File Location"
    End If
End Sub

Funtion:

Code:
Public Function ImportExcel(fileName As String, tableName As String) As Boolean
    On Error GoTo Problem
    
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, tableName, fileName, True
    ImportExcel = True
    Exit Function

Problem:
    MsgBox "The file you tried to import was not an Excel spreadsheet. Please ensure that file is saved as .xls (97-2003)"
    ImportExcel = False
End Function
 
If you return a value from a function the arguments have to be in (). So
if UpdateUsers.ImportExcel(Me.TxtFileName, "ActiveList") = True
or simply
if UpdateUsers.ImportExcel(Me.TxtFileName, "ActiveList") ' since you do not have to say True = True just "is True"

If you do not return a value you cannot use () unless you use the Call Keyword. So
if msgbox ("Some Message",VbYesNo) = vbYes then
but
Msgbox "Some Message",VbInformation
or
Call Msgbox("Some Message",Vbinformation)
 
If you return a value from a function the arguments have to be in (). So
if UpdateUsers.ImportExcel(Me.TxtFileName, "ActiveList") = True
or simply
if UpdateUsers.ImportExcel(Me.TxtFileName, "ActiveList") ' since you do not have to say True = True just "is True"

If you do not return a value you cannot use () unless you use the Call Keyword. So
if msgbox ("Some Message",VbYesNo) = vbYes then
but
Msgbox "Some Message",VbInformation
or
Call Msgbox("Some Message",Vbinformation)


Thanks MajP. That solved the issue.
 

Users who are viewing this thread

Back
Top Bottom