0

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

Mark Tait
  • 545
  • 3
  • 12
  • 22
  • 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 Mar 08 '18 at 17:05
  • 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 Mar 08 '18 at 18:59
  • BTW, tested your code and cannot replicate issue. – June7 Mar 08 '18 at 20:45
  • I suggest doing a full [Decompile](http://stackoverflow.com/a/3268188/3820271). – Andre Mar 09 '18 at 13:31

1 Answers1

0

This is because of a corrupted module.

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

Gustav
  • 53,498
  • 7
  • 29
  • 55
  • No. That's another error. But the cure will be same. Note the marked part of the line includes a space. At least, try to delete that. Otherwise retype. – Gustav Mar 09 '18 at 13:30