Calling a Function

creyn

Board Regular
Joined
Sep 16, 2016
Messages
127
I am trying to call a Function in the Sub of my code. Can someone tell me what I'm doing wrong? Below is the code I am using
Rich (BB code):
Option Compare Database

Public Sub Command0_Click()
Dim MyFolder As String
Dim MyFile As String
Dim MyFolder2 As String
Dim DQ As String
Dim MyData As String
Dim myQueryName As String
Dim myExportFileName As String
 
DoCmd.SetWarnings False
DoCmd.RunSQL "Delete * from Import_Data"
MyFolder2 = "S:\zCorey\TDD File Cleanup\Sample Files\New Version"
MyFile = Dir(MyFolder2 & "\*.txt")
Do While MyFile <> ""
DoCmd.SetWarnings False
DoCmd.RunSQL "Delete * from Import_Data"
DoCmd.TransferText acImportDelim, "Import Specification", "Import_Data", MyFile, True
Call CODEW
'DoCmd.TransferText acExportDelim, "S:\zCorey\TDD File Cleanup\Sample Files\New Version" & [MyFile], False, "", 0
DoCmd.TransferText transferType:=acExportDelim, SpecificationName:="Export Specification", TableName:="Import_Data", _
FileName:="S:\zCorey\TDD File Cleanup\Sample Files\New Version" & [MyFile], hasfieldnames:=True
DoCmd.SetWarnings False
MyFile = Dir
Loop
End Sub


Function CODEW(Character As String, Optional Unicode_value As Boolean = True, Optional Exact_functionality As Boolean = False) As Variant
' Exact Functionality returns exact Unicode for characters as AscW() does
' rather than Windows characters as Asc() does
   Dim Characters As String
   Dim i As Long
   If Exact_functionality Then
       CODEW = AscW(Character)
       If Unicode_value Then CODEW = "U" & Hex(CODEW)
       Exit Function
    End If
   For i = 128 To 159 'where non-compliant
      Characters = Characters & Chr(i)
    Next i
   If InStr(1, Characters, Left$(Character, 1), vbBinaryCompare) Then
       CODEW = Asc(Character)
    Else
       CODEW = AscW(Character)
    End If
    If Unicode_value Then CODEW = "U" & Hex(CODEW)
End Function
 
Last edited by a moderator:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
What do you actually want to do with the function?

That function returns a value, you need to assign swhat it returns to something.
 
Last edited:
Upvote 0
You don't use CALL with a Function (only with procedures). You use Functions like you would with any other built-in function.
Note that the Function has one required arguments (and two optional ones):
Code:
[B][FONT='inherit']Function CODEW(Character As String, Optional Unicode_value As Boolean = True, Optional Exact_functionality As Boolean = False)[/FONT][/B]
So, to use it, it might look something like this:
Code:
MyValue = CODEW("A")
 
Upvote 0
It's probably worth noting that if you do not supply the optional values, the defaults are used (in this case, True and False in that order).
Also, I'd avoid using the Call statement since the value of a UDF (user defined function) or intrinsic function is discarded when you use it. As if that might not be bad enough, function parameters must be supplied when using the Call statement - not sure if that means even the optional ones or not. I agree, there seems to be no reason to call the function at all since the sub seems to end without using it.

You can use the Call statement for functions or subs.
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top