Maldred
Maldred

Reputation: 1104

VBA Compile Error on Function Call

so I'm trying to run this SQL script within a function I call and it's giving me a "Compile Error: Object Required" when I try to run it!

Code calling the function

Private Sub cmdLogin_Click()

   Call Load_Username(Username)

End Sub

Function being called

Private Sub Load_Username(Text As String)

    Dim SQL As String

    Set SQL = "UPDATE tbleUsername " & _
              "SET Username = '" & Text & "' " & _
              "WHERE ID = 1"

    DoCmd.RunSQL SQL

End Sub

Upvotes: 0

Views: 1128

Answers (2)

Mathieu Guindon
Mathieu Guindon

Reputation: 71217

Dim SQL As String
Set SQL = "string literal"

You can't use Set to assign a string literal. Use Set to assign object references. That assignment is illegal, because an object is required. Hence, Object required.

Two possible fixes:

  1. Remove the Set keyword and use the wonderful implicit value assignment syntax.
  2. Replace the Set keyword with the obsolete Let keyword for a long-deprecated explicit value assignment syntax. Only suggesting because I'm seeing you use the long-deprecated Call keyword too.

Upvotes: 3

adarti
adarti

Reputation: 87

I think that you should define variable Username before call function LoadUsername(UserName)

Upvotes: -1

Related Questions