Link to home
Start Free TrialLog in
Avatar of mainrotor
mainrotor

asked on

How do I select a value from a field in an ACCESS unbound datasheet

H Experts,
How do I select a value from a field in an ACCESS unbound datasheet?  My unbound datasheet displays the following fields:  JobID, JobStartDate, JobEndDate, JobAddress.  When  a row/record is selected in the datasheet, I want to put the value of the JobID field in a variable.
I found the following code below, but it only provides the current records position/location in the datasheet (i.e. 1, 2, 3, etc..)

Dim i As Integer
i = Me.dataDisplaySubform.Form.CurrentRecord
MsgBox (CStr(i))

Open in new window


Is it possible to do what I need?  How?  Please provide examples.

Thank you all very much in advance,
mrotor
ASKER CERTIFIED SOLUTION
Avatar of Eric Sherman
Eric Sherman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your data sheet is bound.  Otherwise it couldn't show more than a single row.

You are referring to the CurrentRecord property, that is why you are getting the record's ordinal number.  If you want to get something from a control, you need to refer to the control name.

Me.SaveJobID = Me.subformname.Form!JobID
ET's comment is fine.
Alternative:
i = Me!dataDisplaySubform.Controls("JobID")

Open in new window

Your answer is already given.
You were close, but .CurrentRecord is a property that gives you a number related to your position in the records of an object.  If you had 20 records showing and the cursor is in a control in the ninth row, .CurrentRecord would give you '9'

But your syntax is good up to that point

Dim i As Integer
i = Me.dataDisplaySubform.Form
.CurrentRecord
MsgBox (CStr(i))

Where it gets tricky is after the Form
! operators vs . operators is not well understood (and maybe not even by me -- we'll see if I get corrected here :)
The ! operator gives you the members of a collection
The . operator gives you the properties of an object
Sometimes these two things overlap (Controls are one area of overlap.  Each control is a member of the Controls collection, but they are also defined as properties of the Me object)

You only get Intellisense for the . operator and not for the ! operator.
That trips up a lot of folks when it comes to referencing subforms & subreports
The default collection for a form or report is its Controls collection.

So you want the subform's JobID control
Ok
'I = Me | Mysubform control | treat it as a form
i = Me.dataDisplaySubform.Form.Controls("JobID").Value
That's as explicit as it gets
Now, the Controls collection is the default collection for a form
And the Value property is the default property for a control, so
i = Me.dataDisplaySubform.Form!JobID
is the briefest way of writing the syntax.

Hope this helps

Nick67
>>! operators vs . : Late binding vs immediate binding

Try this in a any event Sub, say Form_Current with a break point at Private Sub line

Me.s.w = 4   ' this immediate binding, it also allow for intellisense to help in writing code.

When you step into procedures it shows an error on .s unless it happens to have a correct values.
Also this error stops you from using immediate window from checking your code.

But with this

Me!s!w =4  'this is late binding, it checks code when time to execute.

Code highlights then when you press Step into again, it displays an error.
You can use immediate window to check code.
How do I select a value from a field in an ACCESS unbound datasheet?
As Pat mentioned here, and I mentioned in your previous question, you're not using an UNBOUND datasheet. If you were, you'd see a single record and nothing more.

I mention this because it's important to use the correct terminology when asking questions like this. An "unbound" form is one that has no Recordsource, and where the controls have no ControlSource. Even if you do not initially "bind" a form to a Recordsource, if you do at some point (and if your controls are bound), then you're using a bound form. In an unbound form, you're entirely responsible for grabbing data from a datasource, and filling the controls. You're further responsible for handling any data edits/additions/deletions. There's rarely a need to do this in Access - and there is never a need in a datasheet form.

If you use the wrong terminology, you run the risk of getting incorrect or misguided advice. In this case, since the participants are all very experienced you don't have much to worry about, but these sorts of things can bite you.

Long story short - be sure you understand the terminology you're using, and make sure you convey that information to the Experts correctly. You'll have a much better EE experience.