Linking Txt Box to a Field in a Table (1 Viewer)

will2

New member
Local time
Today, 11:37
Joined
Jul 27, 2015
Messages
3
I have made a form (frmLogin) that is used as my homepage and has command buttons that take the user to other forms. However, I am trying to make the txt box on the frmLogin be able to filter by a date found in a table (tblDefaults) when the form is opened.

I am trying to pull the value from the tblDefaults form the column "DefDate". Whenever I open the form I get an error that says "Compile error: Invalid use of property." Here is the VBA code that I have written. Also when debugging it highlights ControlSource in my VBA code so that seems to be where the problem is but I can not figure out why.


VBA code:

Private Sub Form_Load()
Dim SQL As String
Dim db As Database
Dim rs As Recordset


strSQL = "SELECT tblDefaults.DefDate FROM tblDefaults WHERE (((tblDefaults.Default)='RetrieveDt'))"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)


'Set Me.txtBS..Recordset= rs
Set Me.txtBS.ControlSource = rs!DefDate


Me.txtBS.Visible = True


Set rs = Nothing
Set db = Nothing
End Sub



Thanks
 

Ranman256

Well-known member
Local time
Today, 11:37
Joined
Apr 9, 2015
Messages
4,339
There is no need for code here. This can be accomplished with a query, say qsAllRecs.
you dont need code at form load. You assign a query to the form.

Once open you can filter using the text box via:
qsQryByDate = select * from table where [date] = forms!frmMain!txtBox

on button click, docmd.openquery "qsQryByDate"

Youre putting too much work into this. No code needed.
 

will2

New member
Local time
Today, 11:37
Joined
Jul 27, 2015
Messages
3
I agree a query would be easier, however, the users of the database want it to be in a VBA code format so the query does not have to run everytime but instead can be pulled from a table to save time.

With that being said, is there a way to make this code work or is a query the only route to go?
 

Users who are viewing this thread

Top Bottom