Combo box - Find a record on a search option

R

Rob W

Greetings,

Using the wizzard for creation of combo boxes the third option onm initial
options page allows the combo box to find a record on a search.

I would like this option but also be able to add to the list to allow new
records to be created.
How can I do this in vba code/form properties? The wizard had a macro
running on the after update, couldnt quite see how to replicate this action
using code/form properties.

This would clear up my problem with my other long winded and hard to read
post !!!

Thanks
Rob
 
C

Cheese_whiz

Hi Rob,

Not positive this is what you mean, but the combo box will reflect any new
records you add to the form automatically. You don't have to do anything to
it.

Post back if that's not what you mean.

CW
 
R

Rob W

HI,

Thanks for the reply.

I will try to explain as clearly as possible.
I have ONE main form with TWO subforms.

The MAIN form is based on the table Module

Table Module consists of the fields : Module Id, Module Name, Archive

There is a combo box on the main form which displaus ONLY the Name but
selects all 3 values from the table.
However if I set the other two text boxes to have a control source using the
combo box column values then I assume they are not bound to the orignial
table fields?
Thus unable to save the info onscreen to a record?

I Ideally want them to use the combo box and be able to SELECT a different
record based on the Module name AND allow them to add new values to create a
NEW record.

If i use the combo box as a search (using combo qizard) it works great to
search but I CANT add new items to the list thus unable to create new
records.

Hope this makes sense
Rob
 
C

Cheese_whiz

Hi Rob,

I'm having a difficult time following you.

The combo box you made with the wizard doesn't do anything but navigate to
records based on what you choose. You edit records with the other controls
on the form. You add records by navigating to a new record, normally by
using a command button or using the button with the * on it in the navigation
buttons at the bottom of the form (assuming you didn't set the form to hide
them).

It sounds like you want to do something different than just edit a record or
add a new record. If so, what?

Sorry I'm so dense....think my coffee fix is wearing off.

CW
 
R

Rob W

Ive tried to strip the system of its 'Access' look and style.

I've made my own navigation buttons and hidden the navigation bar.

So I want a combo box that will SEARCH record AND allow new values to be
edited.

Rob
 
R

Rick Brandt

Rob said:
Ive tried to strip the system of its 'Access' look and style.

I've made my own navigation buttons and hidden the navigation bar.

So I want a combo box that will SEARCH record AND allow new values to
be edited.

If you are using the ComboBox to navigate the main form then you don't need to
reference the other two columns in its RowSource as the ControlSource of the
other two TextBoxes. You should have fields in the form's RecordSource that
they can be bound to and when you navigate with the ComboBox they will display
the values found on that record. Then when you navigate to a new record they
can also be used to make entries in the new record.

If you don't see them changing values as you navigate when they are bound to the
underlying fields then your form is not changing records at all. Displaying the
other columns from the ComboBox in the other two TextBoxes could give the exact
same appearance as navigating (including making your subforms change), but you
could really just be sitting in the same place the whole time.
 
C

Cheese_whiz

Ok Rob,

Let me preface this by saying I personally think it's probably not a good
idea to do it. Also, even if it is 'ok' to do it, there probably is a better
way. However, I wanted to give you an example of how I accomplished it in my
app that you MAY be able to use in yours.

One big caveat: I created a 'bad' record that isn't like any other records
in my query behind the form (read: it's not a 'real' record...it's a made-up
one). I created the record solely for the purpose of being able to run code
when the user picks the record in the combo box. It is important to note
that having this 'bad record' is probably not a good idea. If I were going
to do this in my application for real, I think I would AT LEAST add a new
'filetype' category (filetype is a joined table in my query and is
represented by a control on my form) such that I would have a filetype
category for JUST this file. That way I could exclude it when creating
reports. Otherwise, this 'bad record' would show up in my reports and I just
wouldn't like that.

Ok, the code.

Basically, I added nested 'if' statements to the existing vba code I use in
my combo box that users use to find records. It now looks like this and
works in my app:
__________________________________
Private Sub cmbQuickFile_AfterUpdate()

' Find the record that matches the control.
Dim rs As Object

On Error GoTo cmbQuickFile_AfterUpdate_Error

If Me.Dirty Then
If MsgBox("Save changes to current record before moving to new
record?", vbYesNo, _
"Save changes?") = vbYes Then
Me.Dirty = False
Else
Me.Undo
End If
End If

'Code added to make "New File" work
If Not IsNull(Me.cmbQuickFile.Text) Then
If Me.cmbQuickFile.Column(1) = "New File" Then
DoCmd.GoToRecord acDataForm, "Files", acNewRec
Else
'Stop 'New File Code

Set rs = Forms!Files.Recordset.Clone
rs.FindFirst "[ID] = " & Me![cmbQuickFile]
If Not rs.EOF Then Forms!Files.Bookmark = rs.Bookmark

'Code to end "Ifs" in "New File" code above
End If
End If

Exit_cmbQuickFile_AfterUpdate:
Exit Sub

cmbQuickFile_AfterUpdate_Error:
Call LogError(Err.Number, Err.Description, "cmbQuickFile_AfterUpdate",
Forms!Files.Name, , True)

End Sub
_______________________________

A few notes about the code and the file.

You will have to change the control name to match your form's combo box
control name.

My main form is called 'files'. You will need to change that to the name of
your form.

The record I created in my app uses the term "New File" in the same field
that shows in the combo box that's used to find records. That means that
'New File' is one of the choices in my combo box. The user selects "New
File" in the combo box, and the form navigates to a new record.

The first part of the code isn't relevant to your question, but it addresses
what happens if a user edits a record and then tries to use the combo box
before saving the record (my app requires users to save records manually).

I've marked the code I added with comments (prefaced by the single quotation
mark).

The two lines below "If Me.Dirty Then" in the code should be on one line.
The last character of that line is the underscore character (_), which is the
'continuation' character in vba which allows you to break up a long line of
code into two lines but which will treat it like it is all one line. The
last part of the code also has two lines that split where they shouldn't
have. There is no continuation character in those lines because I didn't
split it up in my code in my app at all.

Basically, you just run separate code if they choose 'New File', and if they
don't you run the 'normal' code for the combo box. In order to get the 'New
File' option, you have to create a record that has 'New File' in the field
that provides the values that display in your combo box used for searching
for records. In your case, I'm assuming that field would be "ModuleName",
though it sounds like you maybe didn't set your combo box up the way it
should have been.

Your combo box should have two columns: The primary key field is Module ID,
and should NOT be visible. The second column should be Module Name, and
those values are what will appear in the combo box and what your users will
choose from to navigate to a record.

From a more practical standpoint, and assuming the name of your recordsource
for the form is named ModuleQuery, then you should select the combo box in
design view of the form (or draw a new one to start over), open the
properties window, and in the data tab, rowsource property type this:

Select [Module ID], [Module Name] from ModuleQuery Order by [Module Name];

The 'bound column' is set to 1. Over on the format tab, the 'column count'
should be 2, and the 'column widths' should be: 0;1"

If you make the appropriate changes (change 'ModuleQuery' to whatever the
table or query is in your database, and make sure to use the proper control
and form names, then this code should work.

As we speak, data managers across the globe are gathering to plot my
assassination for even suggesting a solution like this, but it DOES
accomplish your goal if I understand it correctly.

Sorry for the long post and the misunderstanding. I've just never heard of
anyone using a combo box like that to add new records.

Good luck.
CW
 
R

Rob W

I've given up on it.

The user wanted the add/edit functionality in one screen else it would be
very easy task to acomplish if I could use different forms.

Is there a much easier way to achieve what I want ?

Search and Edit functionality
Add New

???


Cheese_whiz said:
Ok Rob,

Let me preface this by saying I personally think it's probably not a good
idea to do it. Also, even if it is 'ok' to do it, there probably is a
better
way. However, I wanted to give you an example of how I accomplished it in
my
app that you MAY be able to use in yours.

One big caveat: I created a 'bad' record that isn't like any other
records
in my query behind the form (read: it's not a 'real' record...it's a
made-up
one). I created the record solely for the purpose of being able to run
code
when the user picks the record in the combo box. It is important to note
that having this 'bad record' is probably not a good idea. If I were
going
to do this in my application for real, I think I would AT LEAST add a new
'filetype' category (filetype is a joined table in my query and is
represented by a control on my form) such that I would have a filetype
category for JUST this file. That way I could exclude it when creating
reports. Otherwise, this 'bad record' would show up in my reports and I
just
wouldn't like that.

Ok, the code.

Basically, I added nested 'if' statements to the existing vba code I use
in
my combo box that users use to find records. It now looks like this and
works in my app:
__________________________________
Private Sub cmbQuickFile_AfterUpdate()

' Find the record that matches the control.
Dim rs As Object

On Error GoTo cmbQuickFile_AfterUpdate_Error

If Me.Dirty Then
If MsgBox("Save changes to current record before moving to new
record?", vbYesNo, _
"Save changes?") = vbYes Then
Me.Dirty = False
Else
Me.Undo
End If
End If

'Code added to make "New File" work
If Not IsNull(Me.cmbQuickFile.Text) Then
If Me.cmbQuickFile.Column(1) = "New File" Then
DoCmd.GoToRecord acDataForm, "Files", acNewRec
Else
'Stop 'New File Code

Set rs = Forms!Files.Recordset.Clone
rs.FindFirst "[ID] = " & Me![cmbQuickFile]
If Not rs.EOF Then Forms!Files.Bookmark = rs.Bookmark

'Code to end "Ifs" in "New File" code above
End If
End If

Exit_cmbQuickFile_AfterUpdate:
Exit Sub

cmbQuickFile_AfterUpdate_Error:
Call LogError(Err.Number, Err.Description, "cmbQuickFile_AfterUpdate",
Forms!Files.Name, , True)

End Sub
_______________________________

A few notes about the code and the file.

You will have to change the control name to match your form's combo box
control name.

My main form is called 'files'. You will need to change that to the name
of
your form.

The record I created in my app uses the term "New File" in the same field
that shows in the combo box that's used to find records. That means that
'New File' is one of the choices in my combo box. The user selects "New
File" in the combo box, and the form navigates to a new record.

The first part of the code isn't relevant to your question, but it
addresses
what happens if a user edits a record and then tries to use the combo box
before saving the record (my app requires users to save records manually).

I've marked the code I added with comments (prefaced by the single
quotation
mark).

The two lines below "If Me.Dirty Then" in the code should be on one line.
The last character of that line is the underscore character (_), which is
the
'continuation' character in vba which allows you to break up a long line
of
code into two lines but which will treat it like it is all one line. The
last part of the code also has two lines that split where they shouldn't
have. There is no continuation character in those lines because I didn't
split it up in my code in my app at all.

Basically, you just run separate code if they choose 'New File', and if
they
don't you run the 'normal' code for the combo box. In order to get the
'New
File' option, you have to create a record that has 'New File' in the field
that provides the values that display in your combo box used for searching
for records. In your case, I'm assuming that field would be "ModuleName",
though it sounds like you maybe didn't set your combo box up the way it
should have been.

Your combo box should have two columns: The primary key field is Module
ID,
and should NOT be visible. The second column should be Module Name, and
those values are what will appear in the combo box and what your users
will
choose from to navigate to a record.

From a more practical standpoint, and assuming the name of your
recordsource
for the form is named ModuleQuery, then you should select the combo box in
design view of the form (or draw a new one to start over), open the
properties window, and in the data tab, rowsource property type this:

Select [Module ID], [Module Name] from ModuleQuery Order by [Module Name];

The 'bound column' is set to 1. Over on the format tab, the 'column
count'
should be 2, and the 'column widths' should be: 0;1"

If you make the appropriate changes (change 'ModuleQuery' to whatever the
table or query is in your database, and make sure to use the proper
control
and form names, then this code should work.

As we speak, data managers across the globe are gathering to plot my
assassination for even suggesting a solution like this, but it DOES
accomplish your goal if I understand it correctly.

Sorry for the long post and the misunderstanding. I've just never heard
of
anyone using a combo box like that to add new records.

Good luck.
CW

Rob W said:
Ive tried to strip the system of its 'Access' look and style.

I've made my own navigation buttons and hidden the navigation bar.

So I want a combo box that will SEARCH record AND allow new values to be
edited.

Rob
 
R

Rick Brandt

Rob said:
I've given up on it.

The user wanted the add/edit functionality in one screen else it
would be very easy task to acomplish if I could use different forms.

Is there a much easier way to achieve what I want ?

Search and Edit functionality
Add New

I really don't understand your dilemma. EVERY bound Access form provides by
default the ability to do both adding and editing. The fact that you want
to use a different mechanism for navigating is a very tiny insignificant
matter in how it affects this capability.

At the very least you could use your ComboBox for navigation to existing
records and then have a separate button for "Add New". This could all be
done with the wizards and should not take more than about 60 seconds to set
up.
 
R

Rob W

The confusion is my own self doing by wanting to do it all from one control.

I will now add a button.

Cheers
 
K

Khanyisile Mthombeni

How to create a combo box to search for records in a database using Microsoft Access 2007.
 

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