0
votes

I'm trying to get some code to run on a form load.

However, I get the message:

Compile error: Sub or Function not defined

It happens on the line: Public Sub Form_Load()

Option Compare Database

Public Sub Form_Load()
Me.txtSearch.BackColor = vbYellow
  Task = "SELECT * FROM tblTracks WHERE (ID)is null"
        Me.RecordSource = Task
        Me.txtSearch.SetFocus 

End Sub

I've tried Private/Public and having neither - just Sub - but they all give the same error.

enter image description here

I've tried adding Option Explicit and retyping the code from scratch - as suggested - now I have a Compile Error:

enter image description here

Could anyone help please?

Thanks, Mark

1
Start by adding Option Explicit at the top, and adding Dim Task As String in the sub. That would likely either fix the error, or provide you with a more descriptive error. - Erik A
Why bother with the variable since it is used only once? Why are you setting RecordSource on load? Why is RecordSource not set in the form design? Instead of setting RecordSource just to apply filter, I prefer to set form Filter and FilterOn properties. - June7
BTW, tested your code and cannot replicate issue. - June7
I suggest doing a full Decompile. - Andre

1 Answers

0
votes

This is because of a corrupted module.

Remove the full line and retype it from scratch. No copy-n-paste.