Restricting entries in a ComboBox

D

Duncan Edment

I have a Combobox on a form that lists project information. The Combobox is
setup as follows:

ControlSource = tblTimeSheet.fldProjectID
RowSource = SELECT tblProjects.fldProjectDescription, tblProjects.fldProjectID
FROM tblProjects
WHERE (((tblProjects.fldValidUntilDate)>Date()));
BoundColumn = 2

The table, tblProjects, has the following fields:

fldProjectID = Number
fldProjectDescription = Text
fldValidUntilDate = Date / Time
fldIsWork = Yes / No

The basic theory is that each project has a Valid Until Date. After that date,
no more time can be recorded against it.

What I want to do is, show the projects in the ComboBox when it is clicked.
However, if a projects ValidUntilDate is before the current system date--i.e.
now invalid--do not show it in the list for selection. however, it still needs
to be displayed in the database, for those records where is has been entered as
a valid project--i.e. two weeks ago, when it was a valid project code.

Is this possible? I did ask in access.forms, but the solution provided did not
work. Can anyone help?

Many thanks & regards

Duncan

--
Newsgroups are like one big sandbox that all of us
UseNet kiddies play in with peace & harmony.

Spammers, Cross-Posters, and Lamers are the
people that pee in our big sandbox.
 
K

Ken Snell

Duncan -

The Row Source query that you posted should eliminate the display in the
combo box's dropdown list of the projects that have "expired".

I'm not clear about your other desire that you can still show the expired
projects ... where would you be displaying them? In this same combo box as
the value of a field in an already existing record?

Please provide a bit more info.
 
V

Van T. Dinh

You distinguished between "show the projects in the ComboBox when it is
clicked" and "do not show it in the list for selection".

Usually, rows in the combobox are available for selection.

You problably need to explain in details what you want ...
 
G

George Nicholson

One approach (this is semi-air code from memory)
1) Create a text box that is bound to the field. Set Locked to True. Users
won't be editing the textbox value directly.
2) Create an *unbound* combobox, using the Rowsource you've described. Set
Visible to False and place it directly underneath the Textbox from step 1.
And "directly underneath" means just that: stacked on top of each other.
3) In the Enter event for the text box check to see if the combobox is
visible. If it isn't: make the Combobox visible, give it focus, hide the
textbox.
If me.cbo.visible = False then
me.cbo.visible = true
me.cbo.setfocus
me.txt.visible = false
end if
4) In the AfterUpdate event of the combo box, place the newly selected value
into the text box, make the textbox visible, give it focus (the Enter event
triggers again but doesn't do anything since the combo is visible), and then
hide the combo.

If the 2 controls are sized and placed correctly, it will appear to the user
that there is only one control. Any value stored in the db will appear in
the textbox (whether it would be a valid selection now or not), but
new/changed values have to come from the "currently valid" combo.

Alternatively, lock the text box and call a popup form from which user can
select a valid entry. Then transfer the selection back to the textbox.
 
D

Duncan Edment

Ken & Van,

You are right in that the RowSource will prevent any expired projects from being
displayed. I'm quite happy with that. However, let's say we have the following
scenario:

I have been working on Project A, and I have recorded entries similar to the
following:

24/05/04 Project A 09:00 17:00
25/05/04 Project B 08:00 15:45
26/05/04 Project A 09:00 18:16
27/05/04 Project A 09:15 17:25
28/05/04 Project A 09:00 16:35

Project A has now finished and there is no more money in the budget for working
on it, so it has it's ValidUntilDate set to 29/05/04. After this date, no more
work can be recorded against it, as we do not have the money to pay for this
work.

Given the RowSource that I posted:

RowSource = SELECT tblProjects.fldProjectDescription, tblProjects.fldProjectID
FROM tblProjects
WHERE (((tblProjects.fldValidUntilDate)>Date()));

This will indeed prevent any more entries being record against Project A, from
the 29/05/04 onwards, as the project will not be listed in the ComboBox.

However, if I were to go back into the database today and view last weeks work
entries that I completed, I would see the following:

24/05/04 09:00 17:00
25/05/04 Project B 08:00 15:45
26/05/04 09:00 18:16
27/05/04 09:15 17:25
28/05/04 09:00 16:35

Whilst I am prevented from entering any new job entries under Project A, I
cannot see any work that has been completed previously under Project A, when it
was a valid job code.

Have I explained myself better? To quote from Ken's post, yes I want to display
the expired project codes "In this same combo box as the value of a field in an
already existing record?"

Many thanks and regards

Duncan

--
Newsgroups are like one big sandbox that all of us
UseNet kiddies play in with peace & harmony.

Spammers, Cross-Posters, and Lamers are the
people that pee in our big sandbox.
 
T

tina

both of George's solutions will solve your problem. note that either
solution will show the fldProjectID that is saved in the field, rather than
*displaying* the fldProjectDescription in each record on the form. if you
would prefer to display the description while saving the ID, but can't
figure out how, post back for details.

hth
 
K

Ken Snell

In my testing (ACCESS 2002), if you have a continuous forms view for the
form, and you set up a combo box's Row Source to have a limited choice of
items, but the combo box is bound to a field in the table and that field
already contains an item that isn't on the combo box's Row Source dropdown
list, the value that is stored in the field will still display in the combo
box (even though it cannot be selected on the dropdown list) and not cause
any problems. (This is true regardless of whether "Limit To List" property
for the combo box is set to no or yes.)

So, for your example, I would still see Project A in that field, although
Project A would not be seen in the dropdown list.

Therefore, if you're not seeing the already stored info, is there an
additional filter somewhere that you're using?
 
T

tina

if the value being stored in the underlying field is the primary key, but
the value being displayed in the combo box is from another column, how is
that "display" value available if the RowSource record has been filtered
out?
 
K

Ken Snell

If the form's record source includes all records, then the combo box (bound
to a field in that record source) can and will display the value of that
field, regardless of the combo box's row source query's filtering.

If the form's record source is using the same filter statement as is the
combo box's row source, then the item would not be displayed in the combo
box because the entire record will not be displayed in the form -- however,
this is different from what Duncan says he's seeing, as he says he's just
seeing a blank combo box for the record, but the rest of the record is still
shown.
 
K

Ken Snell

OK, tina - After I sent my other reply, I now see what you and Duncan mean
here -- had to read it a few more times for it to sink in. So forget what I
wrote about seeing it and not being able to see it....( < g > )

I agree that either of George's suggestions should be a valid way to do what
Duncan wants.

Other ideas that I would have would still mean being able to see the item in
the dropdown but just not be able to choose it -- which is not a good way to
design a database (in my opinion) as it's not good to "tempt" a user with a
choice that then can't be made -- or would involve display/no display of the
item as the user moves from one record to another -- which might "confuse"
the user who would wonder why a project name is there and not there
depending upon whether the user is on that record or not.

Thanks for "questioning" my note!
 
T

tina

no problem. i tend to take anything an MVP says as gospel - i just figured i
must be crazy because i couldn't figure it out! <g>
 
D

Duncan Edment

George / Tina / Ken,

Thanks for your help on this one however, I'm still stuck! :(

Based on George's suggestions, what I have now is the following:

1. A text field with the following properties:

txtProjectDesc
ControlSource = tblTimeSheet.fldProjectID *
Locked = True

tblTimeSheet.fldProjectID is a numeric field that is joined to the table
fldProjects.

2. A Combobox with the following properties:

cboProjectDesc
ColumnCount = 2
ColumnWidths = 7.699cm;1.27cm
RowSource = SELECT tblProjects.fldProjectDescription,
tblProjects.fldProjectID FROM tblProjects ORDER BY [fldProjectDescription];
BoundColumn = 2

3. txtProjectDesc.OnEnter

If Me.cboProjectDesc.Visible = False Then
Me.cboProjectDesc.Visible = True
Me.cboProjectDesc.SetFocus
Me.txtProjectDesc.Visible = False
End If

4. cboProjectDesc.AfterUpdate

Me.txtProjectDesc.Value = Me.cboProjectDesc.Column(1)
Me.txtProjectDesc.Visible = True
Me.txtProjectDesc.SetFocus
Me.cboProjectDesc.Visible = False

However, I have a couple of problems with this:

1. In the cboProjectDesc.AfterUpdate, I don't think the line that I
have..Me.txtProjectDesc.Value = Me.cboProjectDesc.Column(1)..is correct. It
should be displaying the textual description of the project, not the code that
is associated with the project. How do I achieve this?

2. When I load the form, I select a name from a list and this fills in the
details on the form with the first entry of the last days work entered for the
employee--with me so far! If I then TAB to the Project field--which is
displayed correctly as a text field, but contains the project code, incorrectly,
see 1 above--I then get the following error:

Run-time error: '2110'
can't move the focus to the control cboProjectDesc

Selecting Debug takes me to the following line in the txtProjectDesc.Enter
code:

Me.cboProjectDesc.SetFocus

Within:

If Me.cboProjectDesc.Visible = False Then
Me.cboProjectDesc.Visible = True
Me.cboProjectDesc.SetFocus
Me.txtProjectDesc.Visible = False
End If

Have I done something wrong guys? Am I missing something? Have I totally
mis-read what George has described?

Your help is, as always, appreciated.

Many thanks & regards

Duncan

--
Newsgroups are like one big sandbox that all of us
UseNet kiddies play in with peace & harmony.

Spammers, Cross-Posters, and Lamers are the
people that pee in our big sandbox.
 
T

tina

i couldn't get it to work either, so we're both missing "something". :)
however, i created a test db, using the table and field names you provided
(you have no idea how much that helps!). i set up a "text box on top of
combo box" solution similar to George's, except that it shows the
fldProjectDescription at all times, on both Single Form view and Continuous
Form view. *note: the solution does not work as intended on Datasheet view,
because the text box and combo box will show up as side by side columns
regardless of how they're positioned in Design view.*
here's my tested solution, in case you want to give it a try. (also, if you
want to *see* it, i can email my test db to you.)

1. keep both the text box and the combo box on the form.

2. delete the OnEnter and AfterUpdate code.

3. set the properties of the text box as follows:
Name: txtProjectDescription
ControlSource: =IIf([fldProjectID] Is
Null,Null,DLookUp("[fldProjectDescription]","tblProjects","[fldProjectID] =
" & [Forms]![frmTimesheet]![fldProjectID]))
*note: enter the above, including the = sign, all on a single line in the
ControlSource property. also, check the table, field, form and control names
to make sure they match the names in your database. if they don't, fix
them.*
Enabled: Yes
Locked: Yes
Tabstop: Yes

4. set the properties of the combo box as follows:
Name: cboProjectID
ControlSource: fldProjectID
RowSource: SELECT tblProjects.fldProjectID,
tblProjects.fldProjectDescription, tblProjects.fldValidUntilDate FROM
tblProjects WHERE (((tblProjects.fldValidUntilDate)>Date())) ORDER BY
tblProjects.fldProjectDescription;
*note: enter the above SQL statement all on a single line in the RowSource
property.*
ColumnCount: 3
ColumnWidths: 0";1";0"
*note: widths are in inches - sorry, i don't know the metric conversions,
so you'll have to convert one inch to the appropriate width for your
Description column.*
BoundColumn: 1
Limit to List: Yes
Enabled: Yes
Locked: No
Tabstop: No

5. on the menu bar, click View, TabOrder. set txtProjectDescription *above*
cboProjectID.

6. move the text box directly on top of the combo box *but* drag the right
edge of the text box to the left until you can see the entire "down arrow"
at the right side of the combo box.

7. paste the following code into the form's module. *check all the control
and form names to make sure they match the names in your database - if not,
fix them.*

Private Sub cboProjectID_GotFocus()

If Not Me.NewRecord Then
With Me!cboProjectID
If IsNull(.Column(2)) Then
.Locked = True
End If
End With
End If

End Sub

Private Sub cboProjectID_LostFocus()

Me!cboProjectID.Locked = False

End Sub

Private Sub txtProjectDescription_GotFocus()

With Me!cboProjectID
If .Column(2) > Date Or IsNull(.Value) Then
.SetFocus
End If
End With

End Sub

8. save the form, then close it.

re-open the form, and test it out. my db solution ran without error on a
mixture of "old" project records, "new" project records, and just flat-out
new records. it's not a perfect solution, but as close as i could get. :)

hth
 
K

Ken Snell

Comments inline....

--
- - - - - - - - - - - - - - - - -
Ken Snell
<MS ACCESS MVP>

Duncan Edment said:
George / Tina / Ken,

Thanks for your help on this one however, I'm still stuck! :(

Based on George's suggestions, what I have now is the following:

1. A text field with the following properties:

txtProjectDesc
ControlSource = tblTimeSheet.fldProjectID *
Locked = True

tblTimeSheet.fldProjectID is a numeric field that is joined to the table
fldProjects.

2. A Combobox with the following properties:

cboProjectDesc
ColumnCount = 2
ColumnWidths = 7.699cm;1.27cm
RowSource = SELECT tblProjects.fldProjectDescription,
tblProjects.fldProjectID FROM tblProjects ORDER BY [fldProjectDescription];
BoundColumn = 2

3. txtProjectDesc.OnEnter

If Me.cboProjectDesc.Visible = False Then
Me.cboProjectDesc.Visible = True
Me.cboProjectDesc.SetFocus
Me.txtProjectDesc.Visible = False
End If

4. cboProjectDesc.AfterUpdate

Me.txtProjectDesc.Value = Me.cboProjectDesc.Column(1)
Me.txtProjectDesc.Visible = True
Me.txtProjectDesc.SetFocus
Me.cboProjectDesc.Visible = False

However, I have a couple of problems with this:

1. In the cboProjectDesc.AfterUpdate, I don't think the line that I
have..Me.txtProjectDesc.Value = Me.cboProjectDesc.Column(1)..is correct. It
should be displaying the textual description of the project, not the code that
is associated with the project. How do I achieve this?

Just use Me.cboProjectDesc.Column(0) instead of Me.cboProjectDesc.Column(1).
Column is a zero-based property, so the first column is column 0, the second
column is column 1, etc.
2. When I load the form, I select a name from a list and this fills in the
details on the form with the first entry of the last days work entered for the
employee--with me so far! If I then TAB to the Project field--which is
displayed correctly as a text field, but contains the project code, incorrectly,
see 1 above--I then get the following error:

Run-time error: '2110'
can't move the focus to the control cboProjectDesc

Selecting Debug takes me to the following line in the txtProjectDesc.Enter
code:

Me.cboProjectDesc.SetFocus

Within:

If Me.cboProjectDesc.Visible = False Then
Me.cboProjectDesc.Visible = True
Me.cboProjectDesc.SetFocus
Me.txtProjectDesc.Visible = False
End If

Have I done something wrong guys? Am I missing something? Have I totally
mis-read what George has described?

I believe George meant to say that you should use the OnGotFocus event of
the textbox, not its OnEnter property. Move your code to the GotFocus event
of the textbox and try again.

Note also that there is one "flaw" in George's first suggestion. Namely,
once you've selected the item from the combo box and then the form makes the
textbox visible and the combo box invisible, there is no way that you can go
back to the combo box and change the selection unless you move away from the
textbox and then come back. Therefore, Tina's suggestion to have the textbox
be narrower than the combo box so that you can actually "click" on the combo
box is a more appropriate approach.
 
D

Duncan Edment

Tina,

My head is hurting now! I still cannot get this to work!

If you don't mind, I'll describe--in it's entirety--the form, table(s) and
controls that I'm using, and see if you can help me with it...please?

Table 1 - tblProjects

fldProjectID = Number
fldProjectDescription = Text
fldValidUntilDate = Date/Time
fldIsWork = Yes/No

Table 2 - tblTimesheet

This is the main table that stores all information relating to projects
worked on by employees. The field I am concerned with is:

fldProjectID = Number

This field is linked to the table tblProjects, linking to the fldProjectID
field.

As per your instructions, my main form has two controls on it for the Project
Description:

Control 1 - cboProjectID

ColumnCount = 3
ColumnWidths = 11.401cm;1.801cm;2.501cm
Visible = Yes
ControlSource = tblTimeSheet.fldProjectID
RowSource* = SELECT tblProjects.fldProjectDescription, _
tblProjects.fldProjectID, _
tblProjects.fldValidUntilDate FROM _
tblProjects WHERE _
(((tblProjects.fldValidUntilDate)>Date())) _
ORDER BY tblProjects.fldProjectID;
* I altered the RowSource here to reflect the way the information should
be displayed and sorted.

BoundColumn* = 2
* Once again, I had to alter the value above. With the value you
suggested--1--all I was getting displayed in the field was the Project
code and not the text. This was also making it difficult to select
any values.

LimitToList = Yes
Enabled = Yes
Locked = No
TabStop = No

When I use the code you suggested for the cboProjectID events, the
following happens:

In the GotFocus event, Me!cboProjectID.Column(2) is the ValidUntilDate
field, if I am correct. This being the case, the date is always populated
and so the Locked property of the control cboProjectID will never be set
to "True" here. Am I right?

Control 2 - txtProjectDescription

ControlSource* =
=IIf([fldProjectID] Is Null,Null,DLookUp("[fldProjectDescription]", _
"tblProjects","[fldProjectID] = " & _
Forms!frmTimesheet!fldProjectID))

* So that I understand what is going on here, am I right in thinking:
If the fldProjectID if Null--this would be the one from the main
table--tblTimeSheet--then set the control to Null. Otherwise, lookup
the value in the field fldProjectDescription, within the table
tblProjects,
where the value held in fldProjectID is the same as the value in the
control fldProjectID on the form frmTimesheet.

If my thinking is correct, then the lookup above is wrong. There is no
control on the form named 'fldProjectID'. Did you mean this to be
[tblTimesheet]![fldProjectID]?

Another problem I have noted with this control is when I create a new
record. Tabbing into text field, displays an initial value of '#Name?'
when I try to add a new record. From the ControlSource above, I
would expect that a new record would have a fldProjectID of Null, so
it should display a Null value in the text control. I've tried various
combinations of controls and field values for this, and I just can't get
it to work.

Sorry for the long post!! I just need to get my head around a few things, some
of which I have probably messed up when translating from your code to mine.

The only other thing I miss is the ability to enter data and the combo to match
it with the first entry in its list. But still, I always did want to have my
cake AND eat it!!

Many, many thanks for your help with this Tina, and I patiently await you reply.
If you think your test db will explain things better, please feel free to e-mail
me it. Just remove the underscores and everything in between.

Rgds

Duncan

--
Newsgroups are like one big sandbox that all of us
UseNet kiddies play in with peace & harmony.

Spammers, Cross-Posters, and Lamers are the
people that pee in our big sandbox.

tina said:
i couldn't get it to work either, so we're both missing "something". :)
however, i created a test db, using the table and field names you provided
(you have no idea how much that helps!). i set up a "text box on top of
combo box" solution similar to George's, except that it shows the
fldProjectDescription at all times, on both Single Form view and Continuous
Form view. *note: the solution does not work as intended on Datasheet view,
because the text box and combo box will show up as side by side columns
regardless of how they're positioned in Design view.*
here's my tested solution, in case you want to give it a try. (also, if you
want to *see* it, i can email my test db to you.)

1. keep both the text box and the combo box on the form.

2. delete the OnEnter and AfterUpdate code.

3. set the properties of the text box as follows:
Name: txtProjectDescription
ControlSource: =IIf([fldProjectID] Is
Null,Null,DLookUp("[fldProjectDescription]","tblProjects","[fldProjectID] =
" & [Forms]![frmTimesheet]![fldProjectID]))
*note: enter the above, including the = sign, all on a single line in the
ControlSource property. also, check the table, field, form and control names
to make sure they match the names in your database. if they don't, fix
them.*
Enabled: Yes
Locked: Yes
Tabstop: Yes

4. set the properties of the combo box as follows:
Name: cboProjectID
ControlSource: fldProjectID
RowSource: SELECT tblProjects.fldProjectID,
tblProjects.fldProjectDescription, tblProjects.fldValidUntilDate FROM
tblProjects WHERE (((tblProjects.fldValidUntilDate)>Date())) ORDER BY
tblProjects.fldProjectDescription;
*note: enter the above SQL statement all on a single line in the RowSource
property.*
ColumnCount: 3
ColumnWidths: 0";1";0"
*note: widths are in inches - sorry, i don't know the metric conversions,
so you'll have to convert one inch to the appropriate width for your
Description column.*
BoundColumn: 1
Limit to List: Yes
Enabled: Yes
Locked: No
Tabstop: No

5. on the menu bar, click View, TabOrder. set txtProjectDescription *above*
cboProjectID.

6. move the text box directly on top of the combo box *but* drag the right
edge of the text box to the left until you can see the entire "down arrow"
at the right side of the combo box.

7. paste the following code into the form's module. *check all the control
and form names to make sure they match the names in your database - if not,
fix them.*

Private Sub cboProjectID_GotFocus()

If Not Me.NewRecord Then
With Me!cboProjectID
If IsNull(.Column(2)) Then
.Locked = True
End If
End With
End If

End Sub

Private Sub cboProjectID_LostFocus()

Me!cboProjectID.Locked = False

End Sub

Private Sub txtProjectDescription_GotFocus()

With Me!cboProjectID
If .Column(2) > Date Or IsNull(.Value) Then
.SetFocus
End If
End With

End Sub

8. save the form, then close it.

re-open the form, and test it out. my db solution ran without error on a
mixture of "old" project records, "new" project records, and just flat-out
new records. it's not a perfect solution, but as close as i could get. :)

hth
 
T

tina

see comments "in line".

Duncan Edment said:
Tina,

My head is hurting now! I still cannot get this to work!

If you don't mind, I'll describe--in it's entirety--the form, table(s) and
controls that I'm using, and see if you can help me with it...please?

Table 1 - tblProjects

fldProjectID = Number
fldProjectDescription = Text
fldValidUntilDate = Date/Time
fldIsWork = Yes/No

Table 2 - tblTimesheet

This is the main table that stores all information relating to projects
worked on by employees. The field I am concerned with is:

fldProjectID = Number

This field is linked to the table tblProjects, linking to the fldProjectID
field.

As per your instructions, my main form has two controls on it for the Project
Description:

Control 1 - cboProjectID

ColumnCount = 3
ColumnWidths = 11.401cm;1.801cm;2.501cm
Visible = Yes
ControlSource = tblTimeSheet.fldProjectID
RowSource* = SELECT tblProjects.fldProjectDescription, _
tblProjects.fldProjectID, _
tblProjects.fldValidUntilDate FROM _
tblProjects WHERE _
(((tblProjects.fldValidUntilDate)>Date())) _
ORDER BY tblProjects.fldProjectID;
* I altered the RowSource here to reflect the way the information should
be displayed and sorted.

not sure why you want to show the user all 3 columns, rather than just the
fldProjectDescription. also not sure why you want to sort the list by ID if
you expect the user to choose a project by its' description. in any event,
you can sort by fldProjectID, and still only *show* the description field,
if that's what you'd prefer.

BoundColumn* = 2
* Once again, I had to alter the value above. With the value you
suggested--1--all I was getting displayed in the field was the Project
code and not the text. This was also making it difficult to select
any values.

my instructions called for only one column to *show* in the droplist -
fldProjectDescription. that's why columns 1 and 3 were set to zero width, as
0";1";0"
but from what you described here, you made the adjustments correctly. so if
you want to show the Description, ID and Date in the droplist, it shouldn't
affect the outcome of the code one way or the other.

LimitToList = Yes
Enabled = Yes
Locked = No
TabStop = No

When I use the code you suggested for the cboProjectID events, the
following happens:

In the GotFocus event, Me!cboProjectID.Column(2) is the ValidUntilDate
field, if I am correct.
correct.


This being the case, the date is always populated
and so the Locked property of the control cboProjectID will never be set
to "True" here. Am I right?

no, the date column is not "always populated".
remember you are dealing with a mixture of records, and some of them were
added when a project was active - but now it's not active. so when you look
at a record where the project date is "expired", there will be no records
returned by the combo box SELECT statement - and so no date value in
"Column(2)" for that record.
that was the crux of your initial post: when you restricted the droplist to
"not expired" projects, then the project control was "blank" for any record
with an expired project - because the source record had been filtered out of
the combo box row source.

Control 2 - txtProjectDescription

ControlSource* =
=IIf([fldProjectID] Is
Null,Null,DLookUp("[fldProjectDescription]", _
"tblProjects","[fldProjectID] = " & _
Forms!frmTimesheet!fldProjectID))

* So that I understand what is going on here, am I right in thinking:
If the fldProjectID if Null--this would be the one from the main
table--tblTimeSheet--then set the control to Null. Otherwise, lookup
the value in the field fldProjectDescription, within the table
tblProjects,
where the value held in fldProjectID is the same as the value in the
control fldProjectID on the form frmTimesheet.

correct, though technically i guess it would be more accurate to say that
the expression is refering to the value of the fldProjectID field in the
form's underlying table.

If my thinking is correct, then the lookup above is wrong. There is no
control on the form named 'fldProjectID'. Did you mean this to be
[tblTimesheet]![fldProjectID]?

no, i didn't mean that. as far as i know, you can't refer to a table
directly in that manner.

Another problem I have noted with this control is when I create a new
record. Tabbing into text field, displays an initial value of '#Name?'
when I try to add a new record. From the ControlSource above, I
would expect that a new record would have a fldProjectID of Null, so
it should display a Null value in the text control. I've tried various
combinations of controls and field values for this, and I just can't get
it to work.

the expression i gave you worked fine for me, including the reference to
fldProjectID. but i created my test db as an A2000-format db *using version
A2003*. if you're using version A2000, perhaps it doesn't like the
reference. i changed my references to

=IIf([cboProjectID] Is Null, Null, DLookUp("[fldProjectDescription]", _
"tblProjects","[fldProjectID] = " & _
Forms!frmTimesheet!cboProjectID))

and found that my db is happy with either version. so perhaps the changed
version will work for you.

Sorry for the long post!! I just need to get my head around a few things, some
of which I have probably messed up when translating from your code to mine.

The only other thing I miss is the ability to enter data and the combo to match
it with the first entry in its list.

hmmm, i'm afraid you lost me completely there. if you mean autofill, as in,
you type a "P" and the first selection on the droplist beginning with P
fills in....the solution i built does do that - at least on my pc. :)

But still, I always did want to have my
cake AND eat it!!

Many, many thanks for your help with this Tina, and I patiently await you reply.
If you think your test db will explain things better, please feel free to e-mail
me it. Just remove the underscores and everything in between.

i am going to send you a copy of my db as soon as i post this reply.
hopefully between having my copy to analyze, and the above notes, you'll
have the help you need. if not, post back. we'll keep trying, and maybe
somebody else will step in with better ideas. :)

Rgds

Duncan

--
Newsgroups are like one big sandbox that all of us
UseNet kiddies play in with peace & harmony.

Spammers, Cross-Posters, and Lamers are the
people that pee in our big sandbox.

tina said:
i couldn't get it to work either, so we're both missing "something". :)
however, i created a test db, using the table and field names you provided
(you have no idea how much that helps!). i set up a "text box on top of
combo box" solution similar to George's, except that it shows the
fldProjectDescription at all times, on both Single Form view and Continuous
Form view. *note: the solution does not work as intended on Datasheet view,
because the text box and combo box will show up as side by side columns
regardless of how they're positioned in Design view.*
here's my tested solution, in case you want to give it a try. (also, if you
want to *see* it, i can email my test db to you.)

1. keep both the text box and the combo box on the form.

2. delete the OnEnter and AfterUpdate code.

3. set the properties of the text box as follows:
Name: txtProjectDescription
ControlSource: =IIf([fldProjectID] Is
Null,Null,DLookUp("[fldProjectDescription]","tblProjects","[fldProjectID] =
" & [Forms]![frmTimesheet]![fldProjectID]))
*note: enter the above, including the = sign, all on a single line in the
ControlSource property. also, check the table, field, form and control names
to make sure they match the names in your database. if they don't, fix
them.*
Enabled: Yes
Locked: Yes
Tabstop: Yes

4. set the properties of the combo box as follows:
Name: cboProjectID
ControlSource: fldProjectID
RowSource: SELECT tblProjects.fldProjectID,
tblProjects.fldProjectDescription, tblProjects.fldValidUntilDate FROM
tblProjects WHERE (((tblProjects.fldValidUntilDate)>Date())) ORDER BY
tblProjects.fldProjectDescription;
*note: enter the above SQL statement all on a single line in the RowSource
property.*
ColumnCount: 3
ColumnWidths: 0";1";0"
*note: widths are in inches - sorry, i don't know the metric conversions,
so you'll have to convert one inch to the appropriate width for your
Description column.*
BoundColumn: 1
Limit to List: Yes
Enabled: Yes
Locked: No
Tabstop: No

5. on the menu bar, click View, TabOrder. set txtProjectDescription *above*
cboProjectID.

6. move the text box directly on top of the combo box *but* drag the right
edge of the text box to the left until you can see the entire "down arrow"
at the right side of the combo box.

7. paste the following code into the form's module. *check all the control
and form names to make sure they match the names in your database - if not,
fix them.*

Private Sub cboProjectID_GotFocus()

If Not Me.NewRecord Then
With Me!cboProjectID
If IsNull(.Column(2)) Then
.Locked = True
End If
End With
End If

End Sub

Private Sub cboProjectID_LostFocus()

Me!cboProjectID.Locked = False

End Sub

Private Sub txtProjectDescription_GotFocus()

With Me!cboProjectID
If .Column(2) > Date Or IsNull(.Value) Then
.SetFocus
End If
End With

End Sub

8. save the form, then close it.

re-open the form, and test it out. my db solution ran without error on a
mixture of "old" project records, "new" project records, and just flat-out
new records. it's not a perfect solution, but as close as i could get. :)

hth
 
D

Duncan Edment

Tina,

This is not working!!

I'm using A2003, saving the resulting file in A2000 format.

The first difference that I notice between your database and mine, is that your
text control--txtProjectDescription--appears "on top of" the combo
control--cboProjectID. I also note, that in the Tab Order, the text control
appears before the combo control. This is exactly as you said it should be
done.

However, on mine, the text control is not shown when the form is in design mode.
It appears to be 'behind' the combo control. No matter what I try, I cannot get
the text control to stay on top of the combo control.

If I try to bring the text control to the front--via Format|Bring To Front--the
text control appears at the bottom of the TAB control list. Moving it back up,
to before the combo control, places it "behind" the combo again.

Also, from looking at your database, I notice a difference when a new record is
created. When you press TAB after entering the "TimeSheet ID", the cursor is
placed in the combo control. With mine, it is placed in the text control, and
the text control contains the value "#Name?".

I've checked, double-checked & triple-checked all properties, controls & values
and they match what you have in your database, as well as what you have said in
previous posts. However, it still wont work.

Would you prefer if a sample copy of the database were e-mailed to you, so you
can have a look at it?

Many thanks

Duncan

--
Newsgroups are like one big sandbox that all of us
UseNet kiddies play in with peace & harmony.

Spammers, Cross-Posters, and Lamers are the
people that pee in our big sandbox.

tina said:
see comments "in line".

Duncan Edment said:
Tina,

My head is hurting now! I still cannot get this to work!

If you don't mind, I'll describe--in it's entirety--the form, table(s) and
controls that I'm using, and see if you can help me with it...please?

Table 1 - tblProjects

fldProjectID = Number
fldProjectDescription = Text
fldValidUntilDate = Date/Time
fldIsWork = Yes/No

Table 2 - tblTimesheet

This is the main table that stores all information relating to projects
worked on by employees. The field I am concerned with is:

fldProjectID = Number

This field is linked to the table tblProjects, linking to the fldProjectID
field.

As per your instructions, my main form has two controls on it for the Project
Description:

Control 1 - cboProjectID

ColumnCount = 3
ColumnWidths = 11.401cm;1.801cm;2.501cm
Visible = Yes
ControlSource = tblTimeSheet.fldProjectID
RowSource* = SELECT tblProjects.fldProjectDescription, _
tblProjects.fldProjectID, _
tblProjects.fldValidUntilDate FROM _
tblProjects WHERE _
(((tblProjects.fldValidUntilDate)>Date())) _
ORDER BY tblProjects.fldProjectID;
* I altered the RowSource here to reflect the way the information should
be displayed and sorted.

not sure why you want to show the user all 3 columns, rather than just the
fldProjectDescription. also not sure why you want to sort the list by ID if
you expect the user to choose a project by its' description. in any event,
you can sort by fldProjectID, and still only *show* the description field,
if that's what you'd prefer.

BoundColumn* = 2
* Once again, I had to alter the value above. With the value you
suggested--1--all I was getting displayed in the field was the Project
code and not the text. This was also making it difficult to select
any values.

my instructions called for only one column to *show* in the droplist -
fldProjectDescription. that's why columns 1 and 3 were set to zero width, as
0";1";0"
but from what you described here, you made the adjustments correctly. so if
you want to show the Description, ID and Date in the droplist, it shouldn't
affect the outcome of the code one way or the other.

LimitToList = Yes
Enabled = Yes
Locked = No
TabStop = No

When I use the code you suggested for the cboProjectID events, the
following happens:

In the GotFocus event, Me!cboProjectID.Column(2) is the ValidUntilDate
field, if I am correct.
correct.


This being the case, the date is always populated
and so the Locked property of the control cboProjectID will never be set
to "True" here. Am I right?

no, the date column is not "always populated".
remember you are dealing with a mixture of records, and some of them were
added when a project was active - but now it's not active. so when you look
at a record where the project date is "expired", there will be no records
returned by the combo box SELECT statement - and so no date value in
"Column(2)" for that record.
that was the crux of your initial post: when you restricted the droplist to
"not expired" projects, then the project control was "blank" for any record
with an expired project - because the source record had been filtered out of
the combo box row source.

Control 2 - txtProjectDescription

ControlSource* =
=IIf([fldProjectID] Is
Null,Null,DLookUp("[fldProjectDescription]", _
"tblProjects","[fldProjectID] = " & _
Forms!frmTimesheet!fldProjectID))

* So that I understand what is going on here, am I right in thinking:
If the fldProjectID if Null--this would be the one from the main
table--tblTimeSheet--then set the control to Null. Otherwise, lookup
the value in the field fldProjectDescription, within the table
tblProjects,
where the value held in fldProjectID is the same as the value in the
control fldProjectID on the form frmTimesheet.

correct, though technically i guess it would be more accurate to say that
the expression is refering to the value of the fldProjectID field in the
form's underlying table.

If my thinking is correct, then the lookup above is wrong. There is no
control on the form named 'fldProjectID'. Did you mean this to be
[tblTimesheet]![fldProjectID]?

no, i didn't mean that. as far as i know, you can't refer to a table
directly in that manner.

Another problem I have noted with this control is when I create a new
record. Tabbing into text field, displays an initial value of '#Name?'
when I try to add a new record. From the ControlSource above, I
would expect that a new record would have a fldProjectID of Null, so
it should display a Null value in the text control. I've tried various
combinations of controls and field values for this, and I just can't get
it to work.

the expression i gave you worked fine for me, including the reference to
fldProjectID. but i created my test db as an A2000-format db *using version
A2003*. if you're using version A2000, perhaps it doesn't like the
reference. i changed my references to

=IIf([cboProjectID] Is Null, Null, DLookUp("[fldProjectDescription]", _
"tblProjects","[fldProjectID] = " & _
Forms!frmTimesheet!cboProjectID))

and found that my db is happy with either version. so perhaps the changed
version will work for you.

Sorry for the long post!! I just need to get my head around a few things, some
of which I have probably messed up when translating from your code to mine.

The only other thing I miss is the ability to enter data and the combo to match
it with the first entry in its list.

hmmm, i'm afraid you lost me completely there. if you mean autofill, as in,
you type a "P" and the first selection on the droplist beginning with P
fills in....the solution i built does do that - at least on my pc. :)

But still, I always did want to have my
cake AND eat it!!

Many, many thanks for your help with this Tina, and I patiently await you reply.
If you think your test db will explain things better, please feel free to e-mail
me it. Just remove the underscores and everything in between.

i am going to send you a copy of my db as soon as i post this reply.
hopefully between having my copy to analyze, and the above notes, you'll
have the help you need. if not, post back. we'll keep trying, and maybe
somebody else will step in with better ideas. :)

Rgds

Duncan

--
Newsgroups are like one big sandbox that all of us
UseNet kiddies play in with peace & harmony.

Spammers, Cross-Posters, and Lamers are the
people that pee in our big sandbox.
 
T

tina

hi Duncan. if i understood correctly, you're saying that the database i sent
you works "correctly", or as desired. but yours does not, and you've either
configured it the same - or in some respects haven't been able to get a
specific configuration to "stick".
so it sounds like the version-A2003/format-A2000 setup we're using is the
same, and is working the same. my guess is that your form, or your database
itself, is corrupted.
suggest you open a new database, and STOP. before you do anything else, go
to Tools, Options, General tab and *uncheck* the box beside "Track name
AutoCorrect info". click Apply, then OK. compact the database.
next, import the two tables involved. import the form. create a *new* form,
and copy all your controls, and the code from the VBE window, into the new
form. make the configuration changes to the new form, so it matches the
"working" form i sent you. delete the "old" form, and compact the database.
then try out the new form.
post back with results.

hth


Duncan Edment said:
Tina,

This is not working!!

I'm using A2003, saving the resulting file in A2000 format.

The first difference that I notice between your database and mine, is that your
text control--txtProjectDescription--appears "on top of" the combo
control--cboProjectID. I also note, that in the Tab Order, the text control
appears before the combo control. This is exactly as you said it should be
done.

However, on mine, the text control is not shown when the form is in design mode.
It appears to be 'behind' the combo control. No matter what I try, I cannot get
the text control to stay on top of the combo control.

If I try to bring the text control to the front--via Format|Bring To Front--the
text control appears at the bottom of the TAB control list. Moving it back up,
to before the combo control, places it "behind" the combo again.

Also, from looking at your database, I notice a difference when a new record is
created. When you press TAB after entering the "TimeSheet ID", the cursor is
placed in the combo control. With mine, it is placed in the text control, and
the text control contains the value "#Name?".

I've checked, double-checked & triple-checked all properties, controls & values
and they match what you have in your database, as well as what you have said in
previous posts. However, it still wont work.

Would you prefer if a sample copy of the database were e-mailed to you, so you
can have a look at it?

Many thanks

Duncan

--
Newsgroups are like one big sandbox that all of us
UseNet kiddies play in with peace & harmony.

Spammers, Cross-Posters, and Lamers are the
people that pee in our big sandbox.

tina said:
see comments "in line".

Duncan Edment said:
Tina,

My head is hurting now! I still cannot get this to work!

If you don't mind, I'll describe--in it's entirety--the form, table(s) and
controls that I'm using, and see if you can help me with it...please?

Table 1 - tblProjects

fldProjectID = Number
fldProjectDescription = Text
fldValidUntilDate = Date/Time
fldIsWork = Yes/No

Table 2 - tblTimesheet

This is the main table that stores all information relating to projects
worked on by employees. The field I am concerned with is:

fldProjectID = Number

This field is linked to the table tblProjects, linking to the fldProjectID
field.

As per your instructions, my main form has two controls on it for the Project
Description:

Control 1 - cboProjectID

ColumnCount = 3
ColumnWidths = 11.401cm;1.801cm;2.501cm
Visible = Yes
ControlSource = tblTimeSheet.fldProjectID
RowSource* = SELECT tblProjects.fldProjectDescription, _
tblProjects.fldProjectID, _
tblProjects.fldValidUntilDate FROM _
tblProjects WHERE _
(((tblProjects.fldValidUntilDate)>Date())) _
ORDER BY tblProjects.fldProjectID;
* I altered the RowSource here to reflect the way the information should
be displayed and sorted.

not sure why you want to show the user all 3 columns, rather than just the
fldProjectDescription. also not sure why you want to sort the list by ID if
you expect the user to choose a project by its' description. in any event,
you can sort by fldProjectID, and still only *show* the description field,
if that's what you'd prefer.

BoundColumn* = 2
* Once again, I had to alter the value above. With the value you
suggested--1--all I was getting displayed in the field was the Project
code and not the text. This was also making it difficult to select
any values.

my instructions called for only one column to *show* in the droplist -
fldProjectDescription. that's why columns 1 and 3 were set to zero width, as
0";1";0"
but from what you described here, you made the adjustments correctly. so if
you want to show the Description, ID and Date in the droplist, it shouldn't
affect the outcome of the code one way or the other.

LimitToList = Yes
Enabled = Yes
Locked = No
TabStop = No

When I use the code you suggested for the cboProjectID events, the
following happens:

In the GotFocus event, Me!cboProjectID.Column(2) is the ValidUntilDate
field, if I am correct.
correct.


This being the case, the date is always populated
and so the Locked property of the control cboProjectID will never
be
set
to "True" here. Am I right?

no, the date column is not "always populated".
remember you are dealing with a mixture of records, and some of them were
added when a project was active - but now it's not active. so when you look
at a record where the project date is "expired", there will be no records
returned by the combo box SELECT statement - and so no date value in
"Column(2)" for that record.
that was the crux of your initial post: when you restricted the droplist to
"not expired" projects, then the project control was "blank" for any record
with an expired project - because the source record had been filtered out of
the combo box row source.

Control 2 - txtProjectDescription

ControlSource* =
=IIf([fldProjectID] Is
Null,Null,DLookUp("[fldProjectDescription]", _
"tblProjects","[fldProjectID] = " & _
Forms!frmTimesheet!fldProjectID))

* So that I understand what is going on here, am I right in thinking:
If the fldProjectID if Null--this would be the one from the main
table--tblTimeSheet--then set the control to Null. Otherwise, lookup
the value in the field fldProjectDescription, within the table
tblProjects,
where the value held in fldProjectID is the same as the value
in
the
control fldProjectID on the form frmTimesheet.

correct, though technically i guess it would be more accurate to say that
the expression is refering to the value of the fldProjectID field in the
form's underlying table.

If my thinking is correct, then the lookup above is wrong.
There
is no
control on the form named 'fldProjectID'. Did you mean this to be
[tblTimesheet]![fldProjectID]?

no, i didn't mean that. as far as i know, you can't refer to a table
directly in that manner.

Another problem I have noted with this control is when I
create a
new
record. Tabbing into text field, displays an initial value of '#Name?'
when I try to add a new record. From the ControlSource above, I
would expect that a new record would have a fldProjectID of
Null,
so
it should display a Null value in the text control. I've
tried
various
combinations of controls and field values for this, and I just can't get
it to work.

the expression i gave you worked fine for me, including the reference to
fldProjectID. but i created my test db as an A2000-format db *using version
A2003*. if you're using version A2000, perhaps it doesn't like the
reference. i changed my references to

=IIf([cboProjectID] Is Null, Null, DLookUp("[fldProjectDescription]", _
"tblProjects","[fldProjectID] = " & _
Forms!frmTimesheet!cboProjectID))

and found that my db is happy with either version. so perhaps the changed
version will work for you.

Sorry for the long post!! I just need to get my head around a few
things,
some
of which I have probably messed up when translating from your code to mine.

The only other thing I miss is the ability to enter data and the combo
to
match
it with the first entry in its list.

hmmm, i'm afraid you lost me completely there. if you mean autofill, as in,
you type a "P" and the first selection on the droplist beginning with P
fills in....the solution i built does do that - at least on my pc. :)

But still, I always did want to have my
cake AND eat it!!

Many, many thanks for your help with this Tina, and I patiently await
you
reply.
If you think your test db will explain things better, please feel free
to
e-mail
me it. Just remove the underscores and everything in between.

i am going to send you a copy of my db as soon as i post this reply.
hopefully between having my copy to analyze, and the above notes, you'll
have the help you need. if not, post back. we'll keep trying, and maybe
somebody else will step in with better ideas. :)

Rgds

Duncan

--
Newsgroups are like one big sandbox that all of us
UseNet kiddies play in with peace & harmony.

Spammers, Cross-Posters, and Lamers are the
people that pee in our big sandbox.
 
D

Duncan Edment

Tina,

Hello. It's me. Sore head...match-sticks holding my eyes
open...more coffee inside me than must be good for
me...still looking at this blessed form!

OK, what have I done so far? I've followed your
instructions, to the letter, and created a new database
etc. Still no joy. The control txtProjectDescription
still appears to sit "behind" the control cboProjectID. I
cannot get it to sit "in front" of it.

I think this is the main cause of all my problems. What I
can't understand is, why your database does it OK and mine
doesn't. Right now, I am at work using Access 2000 and
teh database is still in A2000 format. However, this
blessed form still refuses to work.

1. I cannot get the text control to sit in front of the
combo control.

2. The text control still contains the value "#Name?" when
a new record is created.

3. When a new record is created and TAB is pressed to move
to the combo / text control, it would appear as though the
text control is obtaining focus. This would explain why I
can't use the first letter of the project name to move to
the first item in the list.

Many thanks for your patience and understanding

Duncan

--
Newsgroups are like one big sandbox that all of us
UseNet kiddies play in with peace & harmony.

Spammers, Cross-Posters, and Lamers are the
people that pee in our big sandbox.
 
T

tina

well, at this point i am at a loss. i didn't to anything "special" to my
database; in case you're wondering if i used some magic - no. too bad i
didn't, actually, i could just share my secret with you and put us both out
of our misery! <g>
as for the text box getting the focus, in my form it does get the focus when
you tab from the previous field - and then the GotFocus event runs, deciding
whether it is appropriate to move the focus to the combo box, or leave it on
the text box. that's what creates the more-or-less seamless interface for
the user.
you asked in your post yesterday if i would take a look at your database. i
was hoping that my suggestions would work for you then; since you're still
stuck - yes, you can send your database to me if you want, and i'll take a
look.
make a copy of the database, and do the following *to the copy*:
1. delete any proprietary data, and enter a few "dummy" records.
2. *compact the database*.
3. zip to under 1 MB in size.
4. refer to the newsgroups in the email subject line, and email to me at
ttaccKILLALLSPAMess1 at yahoo dot com
removing all the CAPITAL letters.
today is friday, so if you send it today, i'll take a look and try to get
back to you by monday.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top