Invalid Use of Me Keyword (1 Viewer)

Dr_Sherlock

New member
Local time
Today, 08:53
Joined
Apr 16, 2019
Messages
2
Code:
Option Compare Database
Option Explicit
Const sSELECT = "SELECT tblUserLog.UserID, tblUserLog.LogIn, tblUserLog.LogOut " & "FROM tblUserLog "
Const sWHERE = "WHERE (((tblUserLog.LogOut) Is Null)) "
Const sORDER = "ORDER BY tblUserLog.LogIn DESC;"
Dim sSQL As String

[COLOR="Red"]Private Sub cmdAll_Click()
  sSQL = sSELECT & sORDER

  With Me.lstUsers
    .RowSource = sSQL
    .Requery
  End With
  Me.lblUsers.Caption = "Full Log"
End Sub
[/COLOR]

Private Sub cmdClose_Click()
  DoCmd.Close acForm, Me.Name
End Sub

Private Sub cmdCurrent_Click()
  sSQL = sSELECT & sWHERE & sORDER
  With Me.lstUsers
    .RowSource = sSQL
    .Requery
  End With
  Me.lblUsers.Caption = "Currently Logged In"
End Sub




#the above gives me "Invalid use of Me keyword" after I debug the code in the highlighted part. How can I get this fixed?
 
Last edited by a moderator:

pbaldy

Wino Moderator
Staff member
Local time
Today, 08:53
Joined
Aug 30, 2003
Messages
36,124
FYI I moved your thread out of the introductions forum. "Me" is only valid in the code behind a form or report, as it is a shortcut to refer to that object. Sounds like this code is in a standard module, where "Me" can't be used.

Welcome to the site by the way.
 

isladogs

MVP / VIP
Local time
Today, 16:53
Joined
Jan 14, 2017
Messages
18,209
As the code includes 3 command buttons, I'm fairly confident its in a form rather than a standard module.
The SQL appears OK so I think there's a different explanation

Try dimming sSELECT, sWHERE, sORDERBY as String then set the string expressions for each
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 16:53
Joined
Jul 9, 2003
Messages
16,271
I tried the code in a sample database and it works fine. Have you got a list box called "lstUsers" on the Form?
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:53
Joined
Feb 28, 2001
Messages
27,142
Or try defining the constant to have a specific type, as for example

Code:
Const sORDER [COLOR="Red"]As String[/COLOR] = "ORDER BY tblUserLog.LogIn DESC;"

I did that for one of the strings... you can do the same for the other two.

However, there is another test you could perform. Move the entire sub that you highlighted in red to be below / after the Sub_cmdCurrent_Click routine. See if it now calls out the "Current" routine. If so, then you can't use constants in that context and will have to separately DIM and assign values to your strings. And I kind of agree with Colin that there can be issues in using constants like that. I've seen it before but don't at the moment recall the details.

I understand you are trying to minimize your work to avoid repeatedly typing the strings. But first, cut/paste works in the editor so you could type once and cut/paste once. Second, if it is more widespread use than just a couple of references, you could declare the strings in your module's declaration area and then, in the Form_Open routine, assign the values ONCE in that routine. Then they are just like constants except that they are shared variables.
 

Dr_Sherlock

New member
Local time
Today, 08:53
Joined
Apr 16, 2019
Messages
2
Hell Guys,
I get this reply when I open the App "Variable not defined". Where I have the (Form1 in the code <Set oForm = Form1> being Highlighted.
Actually I am trying to build an automatic backup in access. I have the entire code pasted below
I need help.

>> Function RunSub()
Dim sFile As String, oForm As Form
Set oForm = Form1
oForm.Visible = False
End Function

Sub BackUp()
Dim sFile As String, oDB As DOA.Database
sFile = CurrentProject.Path & "" \ "" & Format(Date, "m-d-yy") & ".accdb"
If Dir(sFile) <> "" Then Kill sFile
Set oDB = DBEngine.Workspaces(0).CreateDatabase(sFile, dbLangGeneral)
oDB.Close

Dim oTD As TableDef
For Each oTD In CurrentDb.TableDefs
If Left(oTD.Name, 4) <> "MSys" Then
DoCmd.TransferDatabase acExport, "Microsoft Access", sFile, acTable, oTD.Name
End If
Next oTD
MsgBox "Backup of tables is stored in the folder" & vbCr & "under the file name" & Right(sFile, Len(sFile) - InStrRev(sFile, ""))

End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 08:53
Joined
Aug 30, 2003
Messages
36,124
Why not simply

Forms!Form1.Visible = False
 

isladogs

MVP / VIP
Local time
Today, 16:53
Joined
Jan 14, 2017
Messages
18,209
Do as pbaldy said or use this
Code:
Set oForm = Forms!Form1

EDIT
Dim.... oDB As DAO.Database
 
Last edited:

Users who are viewing this thread

Top Bottom