Page 1 of 3 123 LastLast
Results 1 to 40 of 81

Thread: what is the best way?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780

    what is the best way?

    i have a database and in this database there are different departments. each department has different machines in it, is there a way to show just the department numbers in a VB6 app?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Yes, but we will need more info on your tables and the expected returned data.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    ok each department has a different number of machines in it. each machine has its own number. but they all have to have a department. and i was asked to make it where you can just click on a certain department and veiw all the machines in that department. then be able to click on any of the machines found there. does that help out?

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    i have a table for machines that has the number of the machine the department, and a description of the machine.

  5. #5
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    you could first populate a combo box with all the departments:

    "select distinct department from tblMachines"

    Then code the click event of the combo box to call a procedure that takes the department from the combo box as a paremeter and that would run this sql:

    "select machine, machineDescription from tblMachines where department='" & paremeter & "'"

    then populate your favorite container with that sql (listbox, listview, etc)...if you need actual code, just let us know. cheers
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Is this going to be an Access Form solution?
    If so you could populate a combo box with the distinct names of
    the departments from the departments table. Then add a button
    the execute a query based on the selected combo box item.
    Query to populate the combo box with the departments.
    VB Code:
    1. "SELECT DISTINCT DeptName FROM Departments ORDER BY DeptName ASC;"
    Query to retrieve all machines in the selected department.
    VB Code:
    1. SELECT * FROM Machines WHERE DeptName = ' " & cboDept.Text & " ';"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    the departments are not named they are numbered. i mean they have names but in the DB they are id'd by department number. one more question though, if i use the select statement as shown, won't that give me all the entries. like instead of just showing the 11 departments, won't it give me all the entries in the machine table.

  8. #8
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    no, because of the 'Distinct' clause - no duplicates will be shown
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    ok thanx i see what that means not but i'm not sure how to put that into a list either, sorry i'm terrible at VB. and i am shooting from the hip here, thanx for all your help people.
    VB Code:
    1. lstDept.Additem "SELECT DISTINCT DeptName FROM Departments ORDER BY DeptName ASC;"

  10. #10
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    the following example uses a combo box combined with a listbox to display the machines


    Code:
    dim rs as New ADODB.Recordset
    dim cmdCommand as New ADODB.Command
    dim sql as String
    
    set cmdCommand.ActiveConnection = DBConnection 'valid connection object
    cmdCommand.CommandType = adCmdText
    
    sql = "select DISTINCT deptName, deptID from tblDepartments"
    cmdCommand.CommandText = sql
    
    set rs = cmdCommand.Execute
    
    do while not rs.EOF
        cmbDept.AddItem rs.Fields("deptName").Value
        cmbDept.ItemData(cmbDept.NewIndex) = rs.Fields("deptID").Value
        rs.MoveNext
    loop
    then, you could put this code in the cmbDept_Click event:

    Code:
    call showMachineList(cmbDept.ItemData(cmbDept.ListIndex))
    and then the code for the procedure showMachineList could be:

    Code:
    private sub showMachineList(deptID as Integer)
    
    dim rs as New ADODB.Recordset
    dim cmdCommand as New ADODB.Command
    dim sql as String
    
    set cmdCommand.ActiveConnection = DBConnection
    cmdCommand.CommandType = adCmdText
    
    sql = "select * from tblMachines where deptID = " & deptID
    cmdCommand.CommandText = sql
    
    set rs = cmdCommand.Execute
    
    do while not rs.EOF
        lstMachine.AddItem rs.Fields("machine_name").Value & " - " & rs.Fields("machine_desc").Value
        rs.MoveNext
    loop
    so the above takes the dept names and id's and populates the combo box. Notice that I used the ItemData property of the combo box to store the ID. This is typical as users don't need to see the ID, but rather the name - and YOU need the id so you can query the DB. Hope it is not too confusing (and I just hammered it out without testing it - so there may be syntax flaws - but hey - ya gotta do some of the work! ) cheers

    Have fun!!!
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    thank you so much, i will try it out and let you know if there are errors, just one more question, i already have a record set open for the same form from the same table, so could i just use that recordset?

  12. #12
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    I suppose you could use the same recordset as long as you move to the first record first (rs.MoveFirst). In terms of design however, I find it preferable to use variables private to each sub. I did not include in the code:

    all objects should be set to nothing after they are used, ie.

    set cmdCommand = Nothing

    And if you follow this practice, your app's design will have greater cohesion and will be easier to debug.
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    ok then thanx i'll give it a shot. in my table tho, there is just numbers, the names of the departments aren't given any where in that table. so in light of that is it possible to change the data types?

  14. #14
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    if you only have access to the ID field, then yeah you can use that - no need to use ItemData, you can just assign the number to the list in the combo - I guess I misunderstood and thought you needed both. If the name is in another table, you could probably join the two if you wanted to show both.
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    thanx for all your help, i just have one more question and i think that i can get it. the active connection part. if i have already opened the a connection to the data base do i have to type
    VB Code:
    1. set cmdCommand.ActiveConnection = DBConnection 'valid connection object
    2. cmdCommand.CommandType = adCmdText

  16. #16
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    Yes you do - you need to assign the connection object to the command object's ActiveConnection property...If you think about it this makes a lot of sense....you pass a command to the command object, but it needs to know which database to execute the command against. Typically, you declare your connection object as Public in a module. This way you can keep reusing this object throughout your app and it only needs to be opened once at startup, and then closed upon unload and end.
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    i do have a connection in the module which was why i was asking. i know that it will cause problems if u already have it open and try and open it again. i kinda understand what you mean about the activeconnection property, but if it is possible could you explain it differently. the way that i kinda understand it now is that even tho i have already opened the connection to the DB in my module i still have to set my active connection? am i way off?

  18. #18
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    yes you still have to set the ActiveConnection which is a property of the command object. When you do this, you are not actually opening a connection, but simply creating a reference to a connection object that is already open. Hope that clarifies.
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    ok thanx a bunch, i'll give it all a shot (prolly after lunch) and let you know how it turned out.

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    i took all that code and changed it accordingly and i got an error that says

    invalid property array index.

    can any one help me out on this?

  21. #21
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    if you post your code, I'd be happy to look at it
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    here it is, this part works, i can see all the department names in the combo box.
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim rs As New ADODB.Recordset
    3. Dim cmdCommand As New ADODB.Command
    4. Dim sql As String
    5.  
    6. Set cmdCommand.ActiveConnection = DBConn 'valid connection object
    7. cmdCommand.CommandType = adCmdText
    8.  
    9. sql = "select DISTINCT DeptName, DeptID from Departments"
    10. cmdCommand.CommandText = sql
    11.  
    12. Set rs = cmdCommand.Execute
    13.  
    14. Do While Not rs.EOF
    15.     cmbTest.AddItem rs.Fields("deptName").Value
    16.     cmbTest.ItemData(cmbTest.NewIndex) = rs.Fields("deptID").Value
    17.     rs.MoveNext
    18. Loop
    19.  
    20. End Sub

    this part doesn't work.
    VB Code:
    1. Private Sub Command2_Click()
    2. Call showList(cmbTest.ItemData(cmbTest.ListIndex))
    3. End Sub

    and this is the sub that it calls
    VB Code:
    1. Private Sub showList(deptID As Integer)
    2.  
    3. Dim Trs As New ADODB.Recordset
    4. Dim cmdCommand As New ADODB.Command
    5. Dim sql As String
    6.  
    7. Set cmdCommand.ActiveConnection = DBConnection
    8. cmdCommand.CommandType = adCmdText
    9.  
    10. sql = "select * from tblMachines where deptID = " & deptID
    11. cmdCommand.CommandText = sql
    12.  
    13. Set rs = cmdCommand.Execute
    14.  
    15. Do While Not Trs.EOF
    16.     lstTester.AddItem Trs.Fields("machine_name").Value & " - " & rs.Fields("machine_desc").Value
    17.     rs.MoveNext
    18. Loop
    19. End Sub

  23. #23
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    Okay....just give me a bit of time and I will see what is what - I should be able to post back in about 30 - 45 minutes.
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    i have been testing it out and i found some errors and i figure out if i click on something in the combo box and then try and call the function i get an error that says i'm not passing the required parameters. but if i don't click on it and try to call the function i get the invalid array property error.

  25. #25

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    ok that sounds like a plan. thanx very much for all your help.

  26. #26
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    Well...it looks like there is a mystery on your hands as I did the following:

    I quickly created two tables defined as yours are

    I pasted the code into a project

    It ran without error and did what it was supposed to do


    There must be something else somewhere in your code. If you wish, you may post the project if it is possible. Sorry I don't have an immediate answer for you - but for me it does not crash!!
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  27. #27

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    ok, here is my code.
    VB Code:
    1. Private Sub Form_Load()
    2. Call Connect
    3. Dim rs As New ADODB.Recordset
    4. Dim cmdCommand As New ADODB.Command
    5. Dim sql As String
    6.  
    7. Set cmdCommand.ActiveConnection = DBConn 'valid connection object
    8. cmdCommand.CommandType = adCmdText
    9.  
    10. sql = "select DISTINCT DeptName, DeptID from Departments"
    11. cmdCommand.CommandText = sql
    12.  
    13. Set rs = cmdCommand.Execute
    14.  
    15. Do While Not rs.EOF
    16.     cmbTest.AddItem rs.Fields("deptName").Value
    17.     cmbTest.ItemData(cmbTest.NewIndex) = rs.Fields("deptID").Value
    18.     rs.MoveNext
    19. Loop
    20. End Sub
    21.  
    22. Private Sub Command2_Click()
    23. Call showList(cmbTest.ItemData(cmbTest.ListIndex))
    24. End Sub
    25.  
    26. Private Sub showList(deptID As Integer)
    27.  
    28. Dim Trs As New ADODB.Recordset
    29. Dim cmdCommand As New ADODB.Command
    30. Dim sql As String
    31.  
    32. Set cmdCommand.ActiveConnection = DBConn
    33. cmdCommand.CommandType = adCmdText
    34.  
    35. sql = "select * from tblMachines where deptID = " & deptID
    36. cmdCommand.CommandText = sql
    37.  
    38. Set Trs = cmdCommand.Execute
    39.  
    40. Do While Not Trs.EOF
    41.     lstTester.AddItem Trs.Fields("machine_name").Value & " - " & Trs.Fields("machine_desc").Value
    42.     rs.MoveNext
    43. Loop
    44. End Sub

  28. #28
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    sorry...I meant post the entire project (zipped up)
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  29. #29

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    o ok then. i've never done this before so if you get it let me know ok.
    Attached Files Attached Files

  30. #30
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    I got it and I see the problem right away....

    Code:
    Private Sub Command2_Click()
        Call showList(cmbTest.ItemData(cmbTest.ListIndex))
    End Sub
    you need the above code in the click event of the combo box:

    Code:
    Private Sub cmbTest_Click()
        Call showList(cmbTest.ItemData(cmbTest.ListIndex))
    End Sub
    the idea behind this is to capture the click event of the combo box which will fire when a user selects an item from its list. Now I couldn't run anything, so if there is still a problem, upload db1.mdb, and db2.mdb so I can run it
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  31. #31

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    actually db1 is not needed i got rid of it, so here is db2, i still get the same error about the parameters not being passed. i do however see about the click event tho, that makes complete sense and now i feel like a ding dong for not looking @ that way.
    Attached Files Attached Files

  32. #32
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    alrighty then!!! now we are in business (your gonna kick yourself )

    Code:
    Private Sub showList(deptID As Integer)
    
    Dim Trs As New ADODB.Recordset
    Dim cmdCommand As New ADODB.Command
    Dim sql As String
    
    Set cmdCommand.ActiveConnection = DBConn
    cmdCommand.CommandType = adCmdText
    
    sql = "select * from tblMachines where deptID = " & deptID
    cmdCommand.CommandText = sql
    
    Set Trs = cmdCommand.Execute
    
    Do While Not Trs.EOF
        lstTester.AddItem Trs.Fields("machine_name").Value & " - " & Trs.Fields("machine_desc").Value
        Trs.MoveNext
    Loop
    End Sub

    your sql statement above asks for deptid's from the tblMachines table but you misspelled the field name:

    it is not deptID

    you have it in the db as dept

    After I made the change, there was no error


    also: you will want to add some logic: The listbox continues to grow each time you select an item from the combo - you will want to add a line at the beginning of the sub that populates the listbox that will clear the list first.

    that otta do it (I HOPE!!!)
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  33. #33

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    um...........there must be something wrong with me bc i still get the same error. could you post all the code? i don't understand why i still keep getting the same error no matter what i change, there has to be some thing wrong with my sub call or some thing.

  34. #34

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    never mind about that last post i fixed it. thanx so very much. that helps alot.as far as logic i'll do that later as i have to leave but i'll prolly need help on that too. you've helped out so much, you're the man.....or the woman.

  35. #35
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    no problem chief

    Dave Ahara

    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  36. #36

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    ok now i am going to try to add 2 things to this project. i want to add the logic like you said, i also want to add a small portion that will make it possible to click on the results in the list box and show the info for that specific record. for example if we have a screwdriver that shows up in the list box i want to be able to click on screw driver and then it will load the record for that screw driver. is this possible?

  37. #37
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    First off, I am a firm believer that anything is possible - it just depends on how much time you have!!! Question: Let's say a user clicks on an item in the list box that shows a screw driver....how are you able to identify the screwdriver's matching record in the database? Which table is it in? Is there an ID field that uniquely identifies it and if so, what is it called? This is what you first need to ask yourself. Assuming there is a way to identify items in the listbox, it should be no problem to display the corresponding data. Once you figure out how to identify them, you need a way to get that value when the user clicks on the item. Go back to the combo box and look at how you used the ItemData property to store dept id's. The listbox also supports this property. So when you generate a list in the listbox - you could add additional info such as machinePart ID or whatever it is that you can use to identify the listbox items in the database (you could modify the showList sub). Then, when a user clicks on a listbox item, you call a sub similar to showList, and you show the detail of that record. The only remaining question is what are you going to use to show the detail? You could use textboxes, labels (my old school teaches students to use the picture box - don't ask me why but it does allow you to 'print' data to it).....so hopefully that's enough to get you going....have fun!!
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  38. #38

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    yeah that is eough to get me going. thanx for all the help so far. i just have one more question, i am working on the logic part andi think that i know how to do it, i just need some one to help me out as far a the
    VB Code:
    1. lstbox.ItemData(n)=m
    i saw this in my book and i think that i could use this for the logic part and i need to know about the n and the m. thanx

  39. #39
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    just like with the part you did with the combo box, n is the index of the item in the ItemData collection (m is the value you are adding to it), so if you are populating a listbox, you could use

    Code:
    List1.AddItem "[new value]"
    List1.ItemData(List1.NewIndex) = "[id]"
    the NewIndex property returns the index of an item that has just been added - so in the above example, the new value id will be in the ItemData collection at the same index as the new value that was just added - hence they will be linked by indexes. Its just like what you already did with the combo box.
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  40. #40

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780
    so i couldn't use that for logic then huh, that seems to be the wrong thing for logic. i think that i am going to use and if statement and basically say this
    Code:
    if the list box is empty then
    Do while not trs.eof
    code code
    loop
    else
    clear the list box then
    do while not trs.eof
    code code
    loop
    end if
    is that too redundant?

Page 1 of 3 123 LastLast

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