Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Relating 2 fields in VB (utilizing ACCESS table) 1

Status
Not open for further replies.

TheWave

Programmer
Feb 7, 2000
1
US
I need help in associating 2 fields in a VB program. For instance, if I select a course number (from a drop-down box), I would like the program to automatically populate the course name field. These 2 fields are stored in the same table. I am having difficulty finding any information about doing this because I don't even know what to call it!
 
Well you have to &quot;Move&quot; to the record you want to pull.<br>
<br>
this is done by using a &quot;bookmark&quot;<br>
To move to another record you can use SEEK, if you have a Primary key in your table.<br>
If not you can use FINDFRIST to find the record then you get its' book mark and position to that record.<br>
<br>
syntax as shown:<br>
Sub SeekX()<br>
<br>
Dim dbsNorthwind As Database<br>
Dim rstProducts As Recordset<br>
Dim intFirst As Integer<br>
Dim intLast As Integer<br>
Dim strMessage As String<br>
Dim strSeek As String<br>
Dim varBookmark As Variant<br>
<br>
Set dbsNorthwind = OpenDatabase(&quot;Northwind.mdb&quot;)<br>
' You must open a table-type Recordset to use an index, <br>
' and hence the Seek method.<br>
Set rstProducts = _<br>
dbsNorthwind.OpenRecordset(&quot;Products&quot;, dbOpenTable)<br>
<br>
With rstProducts<br>
' Set the index.<br>
.Index = &quot;PrimaryKey&quot;<br>
<br>
' Get the lowest and highest product IDs.<br>
.MoveLast<br>
intLast = !ProductID<br>
.MoveFirst<br>
intFirst = !ProductID<br>
<br>
Do While True<br>
' Display current record information and ask user <br>
' for ID number.<br>
strMessage = &quot;Product ID: &quot; & !ProductID & vbCr & _<br>
&quot;Name: &quot; & !ProductName & vbCr & vbCr & _<br>
&quot;Enter a product ID between &quot; & intFirst & _<br>
&quot; and &quot; & intLast & &quot;.&quot;<br>
strSeek = InputBox(strMessage)<br>
<br>
If strSeek = &quot;&quot; Then Exit Do<br>
<br>
' Store current bookmark in case the Seek fails.<br>
varBookmark = .Bookmark<br>
<br>
.Seek &quot;=&quot;, Val(strSeek)<br>
<br>
' Return to the current record if the Seek fails.<br>
If .NoMatch Then<br>
MsgBox &quot;ID not found!&quot;<br>
.Bookmark = varBookmark<br>
End If<br>
Loop<br>
<br>
.Close<br>
End With<br>
<br>
dbsNorthwind.Close<br>
<br>
End Sub<br>
<br>

 
Just a side note about using Seek. If you're using ADO to do this instead of connecting to the JET Engine directly as in the above post, it can be extremely slow. I always prefer to use an SQL statement with a WHERE clause to obtain the record. Something more like:<br>
<br>
SELECT CourseNumber, Name FROM tblCourse WHERE CourseNumber=6<br>
<br>
This is just an example, and of course the CourseNumber is redundant in the select statement and I assumed the course number was a numeric data type.<br>
<br>
We had a project that we wrote using SEEK and it was extremely slow. When using SQL like above, we got a speed increase of 7 to 8 times. The only reason we knew it was slow is it was in a loop and had to execute it about 1,200 times.<br>
<br>
Steve<br>
<A HREF="mailto:sdmeier@jcn1.com">sdmeier@jcn1.com</A><br>
<br>

 
The only Problem with SQL (If you could call it a problem)is that it returns a group of records that match a criteria. Say you find all of the &quot;course&quot; as in the original post.<br>
Say there are 25 of them, then you will see only those 25 records.<br>
Now to see the rest of the 100 records altogether you have to create a button(or something to click on) which restores the original recordset.<br>
Like so <br>
SQL = &quot;Select * From MyTable;&quot;<br>
Now Of course this is what I prefer to do. And you are right is it the fastest way to work with Access<br>
but in the original Post <br>
TheWave mentioned &quot;I select a course number (from a drop-down box), &quot; <br>
TheWave wanted, I think to click on the drop down and then jump to that record????<br>
<br>
It all depends on what you want to do.<br>

 
Typically what I would do in a situation with a Combobox is read in a distinct list of courses from the table. Assuming the table is setup with a PrimaryKey that has an AutoNumber field and a Course Number other than the PK. I would read in the courses from the table and put their CourseNumber in the Combobox. Also in the combox, there is another list called ItemData. This is where the Long, CourseID would go. Now, in the cboCourse_Click event, I would use a recordset to do the following:<br>
<br>
&quot;SELECT CourseName FROM tblCourse WHERE CourseID=cboCourse.ItemData(cboCource.ListIndex)<br>
<br>
Then I would put the information in the text box on the form with:<br>
<br>
txtCourseName.Text = rsCourse!CourseName<br>
<br>
For doing single lookups in a small table I don't think you'll find a noticeable speed difference in or the other. I was basing my assumption on he only wanted to populate Course Name field based on the course number. TheWave said: &quot;if I select a course number (from a drop-down box), I would like the program to automatically populate the course name field&quot;<br>
<br>
Weather he keeps and recordset open and seeks, or opens a recordset with a where clause is up to him. I was merely stating that .Seek used repetitively may not be as efficient as a where clause if placed in a loop. I'll try to stick to the topic at hand... Nice explanation of code on your behalf, I thought!<br>
<br>
TheWave, another thing you might consider is instead of using a lookup or seek for this, is to read the CourseNumber and the Course from a recordset and then do a little data tweaking/combining before you put it in the combo box. If you combine the two pieces of information, then you don't need to lookup the Course Name. Like this:<br>
<br>
cboCourse.AddItem rsCourse!Course & &quot; - &quot; & rsCourse!CourseName<br>
<br>
This would produce entries like:<br>
<br>
IM101 - Introduction to Math<br>
AM201 - Advanced Math<br>
CP101 - Beginning Computer Programming<br>
<br>
Then you could choose to sort your list of courses either by the Course Number or by the Course Name. You may have to do some text manipulation if your course numbers are not all the same number of characters. If you're using a true-type font for your combo box, things can get a little tricky with alignment.<br>
<br>
Cheers,<br>
<br>
Steve<br>
<A HREF="mailto:sdmeier@jcn1.com">sdmeier@jcn1.com</A>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top