Results 1 to 7 of 7

Thread: [RESOLVED] Getting Field Values From a Table Using a Foreign Key

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Huntsville, AL
    Posts
    62

    Resolved [RESOLVED] Getting Field Values From a Table Using a Foreign Key

    For simplicity, I have a database with two tables, as follows:

    Code:
    /---------------------------------------\
    | Table Name: GRADES                    |
    |---------------+---------+-------------|
    | Column        | Type    | Comments    |
    |---------------|---------|-------------|
    | ID            | Integer | Primary Key |
    | HazardType_ID | Integer | Foreign Key |
    | Code          | Text    |             |
    \---------------+---------+-------------/
    
    /------------------------------------\
    | Table Name: HAZARDTYPES            |
    |------------+---------+-------------|
    | Column     | Type    | Comments    |
    |------------|---------|-------------|
    | ID         | Integer | Primary Key |
    | HazardType | Text    |             |
    \------------+---------+-------------/
    On my form, I'm trying to display information about a grade. I'm using a DataSet with BindingSources and TableAdapters. Using the tip from here, I'm populating the selected grade's ID, HazardType_ID and Code. Unfortunately, I want to display the the HazardType, and not the foreign key index. I've accomplished this with the following code:

    Code:
    ' get the data
    Dim data As ProjectDataSet.GradesRow = DirectCast(GradesBindingSource.Current, DataRowView).Row
    
    ' fill in form fields
    With data
        lblTypeVal.Text = DirectCast(DirectCast(HazardTypesBindingSource.Item(HazardTypesBindingSource.Find("ID", .HazardType_ID)), DataRowView).Row, ProjectDataSet.HazardTypesRow).HazardType
        txtCode.Text = .Code
    End With
    Is there an easier way of doing this? I'm very new to data driven programs, and I feel like I'm missing something very easy that will revolutionize my understanding.

    Note, I'm using VB Express 2008 (.NET 3.5) and connecting to a MS Access database.

    Thanks.
    Last edited by arcanine; Jul 13th, 2011 at 03:57 PM. Reason: I forgot that was inside a With Statement

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width