Results 1 to 18 of 18

Thread: database help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    109

    database help

    hey guys
    i have a program that is to do with stock. I have a databse that has two tables in it that are linked with BANDID. the first table has BANDID and BANDNAME and the second has BANDID, TYPE and NUMBER.
    BANDID is an autonumber.
    What i need is when the user chooses band from a combobox and writes the name of the item in a text box, i want it to add to the second table with the same ID. i hope this makes sense.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: database help

    Moved to database section.

    A couple of questions:

    What database are you using?

    What platform are you creating the front end in (VB6 VB.NET Something else)?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    109

    Re: database help

    access and vb 6.0

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    109

    Re: database help

    here is my program

    i hope you can see what im trying to do
    Attached Files Attached Files

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: database help

    Not too many of us are willing to download an entire program and look through it to find the code area that you are having questions about.

    You might be better off doing a copy/paste into a post here - with the code in question - so we can direct you properly.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    109

    Re: database help

    it was only to show what im tryin to explain.
    i havent got any code. i really need help with this!

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: database help

    Start by reading Beacon's ADO tutorial.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  8. #8
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    396

    Re: database help

    Hi lauram340

    First I'm going to suggest that you Index the BANDNAME in the Band table. I get the impression from the way your post is worded that the combobox only expects to have band names appear once per band.

    There are probably several different ways you could tackle this. I will provide one that is reasonably easy to understand. To do this you will have to write two queries.

    First: Get the BANDID. Open a Recordset to do this

    VB Code:
    1. yourSql = "SELECT BANDID WHERE BANDNAME = " & yourcomboBox.text
    2. yourBandID = yourRecordset("BANDID")


    Second: Now that you have the BandID you can add the new record. Use the Excute method to do this.

    VB Code:
    1. yourSQL = "INSERT INTO Items (BANDID, ITEM) VALUES (" & yourBandID & ",'"  & yourText.Text & "')"
    2.  
    3. yourDBconnection.Execute (yourSQL)

    If you are not familiar with recordsets and execute commmands, post back and I will provide a complete code sample.
    Last edited by LinXG; Feb 26th, 2007 at 03:30 PM.

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: database help

    @linxg - did you mean to open a recordset in that first code snippet?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    396

    Re: database help

    Hi szlamany

    His comboBox doesn't have the BANDID so he has to retrieve it and a recordset is how I would do this.

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: database help

    My point being that if you look back at your post you don't appear to be actually opening yourRecordset - or am I missing something?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    109

    Re: database help

    hey
    im not very familuar with it.
    sorry. im really embarssed about how little i know.
    do you know what i am trying to do?
    Laura
    x

  13. #13
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    396

    Re: database help

    Hi szlamany;

    Yes you're quite right. I'm assuming that she knows how to do all that but isn't sure quite how to tackle the problem.


    Oops, now I see from a follow up post that she doesn't so I will post a full code example.
    Last edited by LinXG; Feb 26th, 2007 at 03:26 PM.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    109

    Re: database help

    LinXG do you have msn?
    If so add me [email protected]
    it may be easier
    xxx

  15. #15
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    396

    Re: database help

    Here is the sample I promised showing a two step method to do what you want. I haven't tested this code but I think it's good.

    VB Code:
    1. Dim myRecordSet As ADODB.Recordset
    2. Dim myDBconn As New ADODB.Connection
    3. Dim strConn as string
    4. Dim mySql As String
    5.  
    6. Dim myItem as String
    7. Dim myBANDID as Long
    8.  
    9. strConn = "whatever you connection string is"
    10.  
    11.  
    12. '------------------------------------------------
    13. ' First get the band ID
    14. '------------------------------------------------
    15.  
    16. mySql = "SELECT BANDID FROM BAND WHERE BANDNAME = '" & yourCombobox.Text & "'"
    17.  
    18. Set myRecordSet = New ADODB.Recordset        
    19. myRecordSet.Open mySql, StrConn, , , adCmdUnknown       ' Open the recordset
    20.  
    21. myBANDID = myRecordSet("BANDID")                        ' This is where we get the BAND ID
    22.  
    23. myRecordSet.Close
    24.  
    25.  
    26. '------------------------------------------------
    27. ' Now add the Item
    28. '------------------------------------------------
    29.  
    30. myItem = Text1.text                                      ' Item you will add
    31.  
    32.  
    33. mySql = "INSERT INTO Items (BANDID, ITEM) VALUES (" & myBANDID & ",'"  & myItem & "')"
    34. myDBconn.ConnectionString = strConn
    35. myDBconn.Open
    36. myDBconn.Execute (mySql)                                ' This will add the record
    37. myDBconn.Close                              ' Close when done

    You could also try it in one query. Again no garantees

    VB Code:
    1. Dim myDBconn As New ADODB.Connection
    2. Dim strConn as string
    3. Dim mySql As String
    4.  
    5. Dim myItem as String
    6. Dim myBandName as string
    7.  
    8.  
    9. strConn = "whatever you connection string is"
    10.  
    11.  
    12. myBandName = yourComboBox.text                           ' The name of your band
    13. myItem = Text1.text                                      ' Item you will add
    14.  
    15. mySQL = "INSERT INTO Items (BANDID, ITEM) SELECT [Band].BANDID, '" & myItem & "' "
    16. mySQL = mySQL + "FROM [Band] LEFT JOIN Items ON [Band].BANDID = Items.BANDID "
    17. mySQL = mySQL + "WHERE [Band].BANDNAME= '" & myBandName & "'"
    18.  
    19.  
    20. myDBconn.ConnectionString = strConn
    21. myDBconn.Open
    22. myDBconn.Execute (mySql)                                ' This will add the record
    23. myDBconn.Close                              ' Close when done


    I hope this helps you out.
    Last edited by LinXG; Feb 26th, 2007 at 03:33 PM.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    109

    Re: database help

    ahhhhhhh
    this is all so hard.
    i dont know what ADODB is or anything. this is what i got so far

    VB Code:
    1. Private Sub Form_Load()
    2. Dim QuizRS As Recordset
    3. Dim QuizQD As QueryDef
    4. Dim i As Integer
    5.  
    6.  
    7. Set QuizQD = DBName.QueryDefs("Table")
    8. Set QuizRS = QuizQD.OpenRecordset
    9.  
    10. QuizRS.MoveFirst
    11. For i = 0 To 9
    12. cmbbandnames.Text = QuizRS!BANDNAME
    13. QuizRS.MoveNext
    14. Next
    15.  
    16.  
    17.  
    18.    
    19. End Sub


    and none of it works!
    here all im trying to do is set the list in the database to a combobox

  17. #17
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    396

    Re: database help

    Here is how to fill your combo box

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim MyRecordSet As ADODB.Recordset
    3.     Dim MySQL As String
    4.  
    5.     MySQL = "SELECT BANDNAME FROM BAND"
    6.     Set MyRecordSet = New ADODB.Recordset
    7.     MyRecordSet.Open MySQL, yourConnectionString, , , adCmdUnknown
    8.    
    9.  
    10.     Do While MyRecordSet.EOF = False
    11.         yourComboBox.AddItem MyRecordSet("BANDNAME")
    12.         MyRecordSet.MoveNext
    13.     Loop
    14.    
    15.     MyRecordSet.Close
    16.  
    17.    End sub

  18. #18
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    396

    Re: database help

    Hi Laura;

    A couple of things that sometimes slip past people;

    Your connection string will look something like this

    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\yourDatabase.mdb;Persist Security Info=False"
    Also make sure in your project that you have a Project Reference to Microsoft ActiveX Data Objects 2.x Library

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