IF Statement

  • Thread starter Thread starter C#''er
  • Start date Start date
C

C#''er

OK. Im kind of getting this access thing, but Im having a little trouble with
this. I have a table with fields "County" & "LCO", each county belongs to an
LCO and I wish to auto fill a field in an Access form. For example, there is
an LCO named "MIAMI-EAST" and Monroe County belongs to the MIAMI-EAST LCO. I
want it so when you type "Monroe" in the county field, the "LCO" field auto
fills with "MIAMI-EAST". If I were building a windows form in Visual Studio
using C#, It would go something like:

if(countyTextBox.text == "Monroe")
{
lcoTextBox.text == "Miami East"
}

How would I do this in an Access form?
 
You'd be best creating a query that joins your existing table to the table
with fields County and LCO and have the LCO included in the query. Use the
query as the RecordSource for the form, not the table.

An exact translation of your C# code would be

If Me.countyTextBox = "Monroe" Then
Me.lcoTextBox = "Miami East"
End If

but that's really too limiting, since it does nothing if the count is not
Monroe.

(Note, though, that in Access you cannot refer to a text box's Text property
unless the text box has focus. You can refer to its Value property, or just
leave it out, as I did in my example. As well, when you're referring to
controls on a form, you should qualify them. "Me." is an appropriate
qualification if the code that's running is in the Class Module associated
with the form. If not, you'd use "Forms![NameOfForm].")
 
Thanks for the quick reply Doug. The code works great. I do believe your
query idea would be more efficient, but Im having a hard time understanding.
Could you explain more, please?
 
Let's assume the tables are MainTable and CountyTable.

The SQL of your query would be something like:

SELECT MainTable.Field1, MainTable.Field2, MainTable.County, CountyTable.LCO
FROM MainTable INNER JOIN CountyTable
ON MainTable.County = CountyTable.County
 
C#''er said:
Thanks for the quick reply Doug. The code works great. I do believe
your query idea would be more efficient, but Im having a hard time
understanding. Could you explain more, please?

It is essential that you understand these concepts if you want to use Access
or any relational database.
 
Back
Top