Results 1 to 3 of 3

Thread: Trying to get 1 record to reference another on the same table in a Module

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Posts
    4

    Question Trying to get 1 record to reference another on the same table in a Module

    I'm trying to write a module in Access 97 that takes a record that is predefined, compares it to a table, finds a corresponding record on that table and displays that corresponding record as my result.

    My table (named "Records")looks like this:

    Table name: Records
    Code:
    Predefined  | Corresponding
    A                    B

    I thought I could do this using SELECT FROM & WHERE, but I think I've been going about this all wrong. Here's what I've been trying to get work:

    Code:
    Private Sub Test1_Click()
    
        Dim myrecord As String
        
        myrecord = "SELECT tblRecords.Corresponding FROM tblRecords WHERE tblRecords.Predefined = A"
        
                          
        MsgBox ([myrecord])
        
        Exit Sub
    I want B as a result but all that I'm getting as the result in my Message Box is "SELECT tblRecords.Corresponding FROM tblRecords WHERE tblRecords.Predefined = A " and not the record I was looking for.

    What am I doing wrong? Am I going about this the completely wrong way?
    Last edited by mehmet; Aug 30th, 2004 at 02:00 PM.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Trying to get 1 record to reference another on the same table in a Module

    Originally posted by mehmet
    I'm trying to write a module in Access 97 that takes a record that is predefined, compares it to a table, finds a corresponding record on that table and displays that corresponding record as my result.

    My table (named "Records")looks like this:

    Table name: Records
    Code:
    Predefined  | Corresponding
    A                    B

    I thought I could do this using SELECT FROM & WHERE, but I think I've been going about this all wrong. Here's what I've been trying to get work:

    Code:
    Private Sub Test1_Click()
    
        Dim myrecord As String
        
        myrecord = "SELECT tblRecords.Corresponding FROM tblRecords WHERE tblRecords.Predefined = A"
        
                          
        MsgBox ([myrecord])
        
        Exit Sub
    I want B as a result but all that I'm getting as the result in my Message Box is "SELECT tblRecords.Corresponding FROM tblRecords WHERE tblRecords.Predefined = A " and not the record I was looking for.

    What am I doing wrong? Am I going about this the completely wrong way?
    That's because that's all you are doing with it. You haven't actualy executed the SELECT.

    I'm going to go out on a limb and say "Yer new to DB programming aren't you?"

    Try reading these articles they walk you through some of the basics of ADO. Ignore the part about SQL Server in the intro, they actualy use Access in the examples.

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Posts
    4

    Re: Re: Trying to get 1 record to reference another on the same table in a Module

    Originally posted by techgnome

    I'm going to go out on a limb and say "Yer new to DB programming aren't you?"
    I don't think that's really going out on a limb.

    Thanks for the link.

    edit:
    I edited my original code to be like this:
    Code:
    Private Sub test1_click()
    dim rst as DAO.recordset
    dim db as DAO.Database
    
    dim strSQL as string
    strsql="SELECT tblRecords.Corresponding FROM tblRecords WHERE tblRecords.Predefined = 'A'"
    set db=currentdb
    set rst=db.openrecordset(strSQL)
    msgbox rst.fields("Corresponding").value
    end sub
    Now I'm getting "Item not found in this Collection" as my error. Any idea why? I have values for both "Predefined" and "Corresponding"

    edit: nevermind, I got it. I was messing up my syntax. Thanks!
    Last edited by mehmet; Aug 30th, 2004 at 04:54 PM.

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