Results 1 to 4 of 4

Thread: How lost...well databases arnt my forte

  1. #1
    ^nighthawk^
    Guest

    How lost...well databases arnt my forte

    Ok Yet Another Problem ..Bleh


    Ok the following code "was" (cause it aint workin)
    to assign a Competitor to an Event.

    No here's the details.

    Database created in access 97.
    3 Tables

    CompetitorTable
    EventTable
    CompetitorEvent

    Competitor Attributes:
    Competitor Number - Primary Key
    Junk Etc

    Event Attributes:
    Event Code - Primary Key
    More Junk etc

    CompetetorEventTable Attributes:
    Event Code - (meant to be some sort of link to event table)
    Competitor Number - (meant to be some sort of link to competitor table)

    Ok. Thats that
    .
    other details:

    I've tried alternating primary keys, making them both primary keys, making neither primary keys etc.

    EventTable.EventCode has a 1-To-Many-Relationship with CompetitorEventTable.EventCode

    CompetitorTable.CompetitorNumber has a 1-To-many-Relationship with
    CompetitorEventTable.CompetitorNumber

    Anyway all this leads to is the following code wont work because
    "You cant add a record or change a record because a related record is required in EventTable".

    -The records are present in both other tables btw. This table is just supposed to relate the two :/


    Code:
    Dim sSelected As String
    Dim sCode As String
    
    sSelected = frmRegisterCompetitor.cboCompetitorNumber.Text
    sCode = frmRegisterCompetitor.cboEventCode.Text
    
    Dim DB As Database
    Dim RS As Recordset
    
    'Use the OpenDatabase function to open our database
      Set DB = OpenDatabase("E:\Assignment3\Games.mdb")
    
    'Open the Contact table
      Set RS = DB.OpenRecordset("CompetitorEventTable", dbOpenDynaset)
    
     With RS
        'Set it to Add mode
        .AddNew
        
        'Enter the field values
        .Fields("EventCode").Value = sSelected
        .Fields("CompetitorNumber").Value = Val(sCode)
        
        'Update it
        .Update
    
        'Close it
        .Close
        
      End With
    
        Set RS = Nothing
        DB.Close
        Set DB = Nothing
    
    End Function

    Anyhelp is appreciated, maybe a run down on how i've got lost or am misunderstanding, and what statements it would accept instead would be good.

    I can change the database in access if need be, but it is vital the tables are not expanded or reduced

    Just need the info

  2. #2
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Aren't your 2 strings (sCode and sSelected) the wrong way round when you assign them?

    Code:
    sSelected = frmRegisterCompetitor.cboCompetitorNumber.Text
    sCode = frmRegisterCompetitor.cboEventCode.Text
    then

    Code:
    .Fields("EventCode").Value = sSelected
    .Fields("CompetitorNumber").Value = Val(sCode)

    J.

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    hi

    Hi,

    Don't think you need .value on the fields...

    rs("fieldname")=value

    Although you'll need the right types putting in.

    Could I suggest Clng function for getting a long value from a string? (perhaps)

    And I think Grantly is right

    Regards
    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  4. #4
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    St. Albans, Herts, UK
    Posts
    259
    The error "You cant add a record or change a record because a related record is required in EventTable". is because there is a relation set up between the tables, so you need to either disable the relation or make sure the EventTable is fed the right data first.
    Graham, www.gab2001uk.com VBExplorer Forum Moderator VBExplorer
    www.gab2001uk.com For comparing and contrasting DAO with ADO
    Code for Creating, Copying, Compacting, Replicating, Synchronising Access 97/2000 databases plus showing Schemas and using .Seek

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