pushing data from recordset to form fields

GoodLife22

Registered User.
Local time
Today, 17:23
Joined
Mar 4, 2010
Messages
86
I am opening a form with the following code:

Code:
Public Sub getEmployee(sEmployee As String)
    Dim db As dao.Database
    Dim sSQL As String
    Dim sRS As dao.Recordset
    
    sSQL = "SELECT * FROM tbl__STAFF WHERE INITIALS='" & sEmployee & "'"
    Set db = CurrentDb()
    
    Set rs = db.OpenRecordset(sSQL)
         
    MsgBox ("Name: " & rs!Name)
    MsgBox ("Phone: " & rs!PHONE)
    MsgBox ("Fax: " & rs!FAX)
    MsgBox ("Email: " & rs!EMAIL)
    
    'txtName.Value = rs!Name
    'Me.txtName = rs!Name
    'txtPhone.Text = rs!PHONE
    'rs!FAX = Me!txtFax.Text
    'rs!Email = Me!txtEmail.Text
    
    rs.Close
    Set rs = Nothing


The form is bound to a different query so I am running this recordset to use in one other section of the form. My sSQL query runs perfect currently and my message boxes show the correct values.

Now I want to create 4 unbound fields on my form and show all of the information from the message boxes.

I created four unbound fields and named them (txtName, txtPHONE, txtFAX, txtEmail) however when I open the form none of the data is in these fields.

So should I be doing something other than an unbound field? Is it the way I am naming my unbound fields? Since I have the value of rs!Name, rs!PHONE, rs!Fax, & rs!Email (I know the info is there, I can see it in my message boxes) how do I get them onto my form?

Thanks.
 
Morning! Couple of things I noticed:

Doesn't look like you're selecting a record in your recordset

The code you're using to populate your text boxes is commented out

A couple of the lines are the wrong way round:
Code:
'rs!FAX = Me!txtFax.Text
    'rs!Email = Me!txtEmail.Text
Should be
Code:
'Me!txtFax.Text = rs!FAX
    'Me!txtEmail.Text = rs!Email
 
OK, thank you for the help so far. A couple things

1.) The "sEmployee" is coming from another form via a button click. So when the SQL query is launched it already knows the value of sEmployee. As I mentioned earlier I know the SQL statement works because it will show me the values via the message boxes.

2.) I commented out all of the code out just to show what all I tried that did not work.

I did:
Me!txtFax.Text = rs!FAX
with no luck. I get an error of:

You can't reference a property or method for a control unless the control has the focus.

Any other thoughts?
 
Apologies, pre-morning coffe thread answering never good :)

Try .value instead
 
that did it. I am working fine now. Funny part is I was pulling the report in Print Preview mode and the text fields would now show that way, but once I switch it to normal mode they look good. Most likely just a buggy print driver issue.

Either way I am good. Thank you James!
 
Probably.... if that problem continues though might be worth starting a new thread.

Anyhoo no worries, have a good afternoon :)
 

Users who are viewing this thread

Back
Top Bottom