views:

340

answers:

2

Right now I am working on a new database that will show changes to one of our manuals that people request. I have two tables and one form. The first table is were all the information will be stored from the reference or subchapter of the manual, the current text in that subchapter, and what the proposed changes are. The second table is for subchapter reference with three fields: ID, SubChapter, and a memo field with what the subchapter says. Right now I am using a combo box for the chapter reference and depending on the reference it fills in another field with current text from another field. Here is my if statement to get a better idea:

If code_ref = "82.101(c)" Then
current_code = "Other County Regulations"
proposed_code.SetFocus
End If

What I would like to do is turn those thousand if statements into a lookup to where it checks the reference and then inserts the proper text in the next field. I am having trouble trying to find a way to do this without just finishing the if statements. I want to do it on change so they can cycle through the reference to find exactly what they are looking for. Any suggestions?

Taken from my comment: An idea of how this will work will be they open the form fill out their name, the item it falls under, use the combobox to select the code reference, once they select the code reference the current code field box will be filled in for them and it will set focus on the proposed code field so they can make their changes.

A: 

You need a look-up that is table-based. From your description I'm not sure if this calls for a 3rd table, or an additional field in the 2nd table.

http://www.trigonblue.com/AccessLookup.htm
http://www.fabalou.com/Access/General/lookup%5Ftables%5Flut.asp

Do not use "LookUp Fields":
http://www.mvps.org/access/lookupfields.htm

Smandoli
+3  A: 

Is the reference available in a table, and if not, can you build such a table:

code_ref     current_code
82.101(c)    Other County Regulations

With such a table, you should be able to create a query that can be used to show the current_code when code_ref is selected.

Edit re Comment

You may be able to create a query that selects data from both tables and then use the wizard to add a combobox to navigate through the records. If you have unique keys, the recordset may be editable.

You may be able to set up a combobox that can be used to DLookUp the relevant table:

Me.txtTextBox = DLookUp("FieldYouWantToReturn","TableNameHere","current_code='" _
   & Me.cboCombobox & "'"

The way DlookUp (briefly) is:

DLookUp("FieldYouWantToReturn","TableNameHere","NameOfTextFieldInTable='" _
       & Me.ControlOrValueToCompare & "'"

Me is shorthand for the current form
Note that the control is outside of the quotes
If you want to compare a number, you do not need quotes
If you want to compare a date, you need hash marks #yyyy/mm/dd#

You may be able to use a subform, with current_code for the link fields.

These are just a few ideas, it is not quite clear what you want the user to be able to do.

Remou
Yes, that is how the second table is set up with a unique id field as well.
Dennis
I have added a few notes.
Remou
An idea of how this will work will be they open the form fill out their name, the item it falls under, use the combobox to select the code reference, once they select the code reference the current code field box will be filled in for them and it will set focus on the proposed code field so they can make their changes.
Dennis
This idea is great, but a text field will only allow me 255 characters and some of the reference is much over that limit.
Dennis
A textbox is a control, not a text field and will accept all the data from a memo field. Is that what you mean?
Remou
I have been trying to implement your idea but I am having trouble. This is what I have: Private Sub code_ref_AfterUpdate() current_code = DLookup("[title]", "ChapterCallout", "[code_ref]=Forms![ChapterCallout]![chapter_callout]") End SubThe combobox is labeled code_ref in the form.
Dennis
Please see my notes on dlookup.
Remou
Dennis
That sounds about right. You need to make sure that the code_ref combobox either has a bound column equal to something you will find in current_code or you use the column property of the combobox - this is only important if you have more than one column in the combo.
Remou
What do you mean by "code_ref combobox either has a bound column equal to something you will find in current_code"? I have two columns in the table 1 being the subchapter callout and 2 being the current code description. If I use the bound column that is equal to the current code which would be 2 then will it not show the description instead of the callout?
Dennis
So say your combo rowsource is: SELECT code_ref, current_code FROM Table ; bound column is 1 ; column count is 2 ; you can refer to the second column using the column property - Me.code_ref.Column(1) ; start counting columns from zero for this property.
Remou
Now that makes sense, so where would I put the DLookUp() expression in my database?
Dennis
It goes in code (VBA, After Update event for the combo, possibly Currrent event for the form) if you want the textbox to be updateable or if the textbox is bound, or in the Control Source for the textbox, if updateable is not needed. If the textbox is not bound, you will need to write to a table with, say, an update query.
Remou
Dennis
Remou
The underscore ( _ ) is a line continuation character used to break up code lines. Me is not used in controls, only in code. There is a lot to be said to naming your controls something other than the field name, so, for example, code_ref would become cboCode_ref, this prevents controls being confused with fields (columns).
Remou
That solved most of the problems but I am still getting an error saying invalid syntax. "You may have entered an operand without an operator"
Dennis
Oops missing ampersand ( If you are having difficulties with anything of this nature, it is worth testing with an actual value : DLookUp("[title]", "ChapterCallout", "current_code='Other County Regulations'")
Remou
that is actually what I am doing right now. It seems that the criteria is what is not going through. I am using the intermediate window to test the code. But adding the ampersand is what helped. Now another issue, sorry for all this. But, I am not getting the information in the current_code field. And when I add the actual value all it shows is error. Any suggestions?
Dennis
Run that by me again, please. When you run in the immediate window with an actual value, does it work? Do you need a column reference, as discussed previously?
Remou
I can run the expression and domain fine and it shows up as the first record in the table, its when I add the criteria that it gives me errors. And by adding the column reference you mean code_ref.Column(1) 1 being actually the second column cause everything starts with zero.
Dennis
You mean adding "current_code='Other County Regulations'" gives errors? Which error number and description? Yes, that is the column reference I meant.
Remou
Actually I am getting two depending on how I put it, when [] are put around code_ref it comes up with external name not defined, and when the [] are not around code_ref I receive the error 2001. That is in the immediate window. But when I put it in the control source it does not give me any error but the current_code field stays blank.
Dennis
Things are different depending where you are, in the immediate window, you must refer fully to the form and the form must be open : Forms!NameOfForm!current_code ; this is one reason I suggested using the actual value, to see if we have got the reference working ok. In the control source, I suspect you must reference the column, you can test this in the immediate window, like so : ?Forms!FormNameHere!current_code.Column(1)
Remou
That is when I come up with the error 424. And current_code is a field in the Changes table. ChapterCallout is a table with code_ref and title (being the information I want to retrieve.
Dennis
This is getting too complicated to explain over the phone :) Can you upload your file to a free site or else post your email?
Remou
[email protected] is my email address and I can upload the file to a server we have here at work. Trust me I think I am making this a lot harder then it really is.
Dennis