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

Thread: multiple record info from access DB

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Question multiple record info from access DB

    Hi, i'm a prepper and i was just wondering..

    if an access database has 3 fields... Name, Required & On Hand.. so, if say i selected "Paper Goods" from combo1.. the NAME gets select.. but i have a lblRequired and lblOnHand.. I want those to get the "REQUIRED" AND "ON Hand" numbers for that record entry

    how do you use the:
    cmd.CommandText = "Select Name from Bathroom order by Name" 'at the moment i can only get all the NAMES into the combox.. but when i select the name - i want the other 2 pieces of info to show in the 2 labels


    any suggestions?

    thank you

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    sample image:
    Attachment 192640

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    err.. "shampoo" in this case..

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    sample code:
    Code:
    Sub test()
        dbConnect
        Dim cmd As ADODB.Command
        Set cmd = New ADODB.Command
        Set cmd.ActiveConnection = cnn
        cmd.CommandText = "Select Name from Bathroom order by Name" 'this is where i'm stuck
        Set rs = cmd.Execute
        Do While Not rs.EOF
            Combo1.AddItem (rs!Name)
            rs.MoveNext
        Loop
        Combo1.ListIndex = 0 'k, so it adds the names to the combobox..
        rs.Close
        cnn.Close
        
        Label2.Caption = "Pantry / Bathroom" 'NEEDS TO BE EDITED
        Combo1.Text = "-- SELECT --"
        Frame1.Visible = "True"
        Command2.Visible = "False" 'EDIT button
      
    
    'label1
    'label2
    
    End Sub

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,736

    Re: multiple record info from access DB

    2 ways
    1) Add the ID to your first query, and store it in ItemData of the Combobox-Item. After clicking on a Combobox-Entry you just fire off a second query, grabbing those fields corresponding to that ID (which is in ItemData)
    2) Select all 3 Fields in your first query from the get-go, and add them to an array. After clicking on a Combobox-Item grab the other values from the array using the ItemIndex as the Array-Index
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,500

    Re: multiple record info from access DB

    Choice #2 would be MY choice....always do it that way!
    Sam I am (as well as Confused at times).

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    that would be fine and and great.. yet i don't know how to do it. do i add commas or an "&" after each:

    cmd.CommandText = "Select Name, Required, On Hand from Bathroom order by Name" 'this is where i'm stuck ??????????????????????????????????

    how would i put it in array? if i knew how to do it i wouldn't be here asking.. demo code anyone? ty


    i can post the project if needed... it's a very simple app i think

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,500

    Re: multiple record info from access DB

    post it.

    Please change your field named "Name" in your database to something else...many times 'Name' is a reserved codeword....just to be safe!
    If "On Hand" is a field name in your db, you'll need to surround it with brackets...like this:

    Select itemName, required, [On Hand] from bathroom order by itemName
    Sam I am (as well as Confused at times).

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,500

    Re: multiple record info from access DB

    As far are putting the records into an Array, something like this

    Code:
    Dim sArray() as String '(you may have to REDIM)
    Do While Not rs.EOF
        sArray(0) = rs!itemName
        sArray(1) = rs!required
        sArray(2) = rs![On Hand]
        rs.movenext
    loop
    Sam I am (as well as Confused at times).

  10. #10
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,736

    Re: multiple record info from access DB

    Quote Originally Posted by SamOscarBrown View Post
    As far are putting the records into an Array, something like this

    Code:
    Dim sArray() as String '(you may have to REDIM)
    Do While Not rs.EOF
        sArray(0) = rs!itemName
        sArray(1) = rs!required
        sArray(2) = rs![On Hand]
        rs.movenext
    loop
    Errr....not exactly, Sam.
    It should be a 2D-Array.
    Kinda like (Aircode)
    Code:
    Dim sArray() As String
    Dim i As Long
    Redim (0 To RS.RecordCount-1, 0 To RS.Fields.Count-1)
    
    For i=0 To UBound(sArray,1)
       sArray(i,0)=RS!itemName
       sArray(i,1)=RS!required
       sArray(i,2)=RS!OnHand
    Next
    Be careful with Datatypes.
    An alternative might be an 1D-Array of an UDT, containing the correct Types
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  11. #11
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,500

    Re: multiple record info from access DB

    Or, just do it like this example...no arrays. Simple queries.

    BlakeSheldonExample.zip
    Sam I am (as well as Confused at times).

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    can't use the other "rooms".. only loads 1 from list and then can't choose another room
    BlakeSheldonExample.zip
    Last edited by BlakeSheldon; Sep 2nd, 2024 at 06:07 PM.

  13. #13
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,500

    Re: multiple record info from access DB

    I Know....I only put one "ROOM" in my example database.
    Sam I am (as well as Confused at times).

  14. #14
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,500

    Re: multiple record info from access DB

    In your program, add Combo1.Clear see below:

    Code:
     Private Sub List1_Click()
          
            Combo1.Clear        
            roomName = Trim(List1.Text)
            Set cmd = New ADODB.Command
            Set cmd.ActiveConnection = cnn
    
    
            cmd.CommandText = "select itemName from " & roomName & " Order by itemName"
            Set rs = cmd.Execute
            If rs.RecordCount = 0 Then Exit Sub
            Do While Not rs.EOF
                    Combo1.AddItem (rs!itemname)
                    rs.MoveNext
            Loop
            Combo1.ListIndex = 0
            Combo1.Visible = True
            Label1(1).Visible = True
            Label1(2).Visible = True
            lblRequired.Visible = True
            lblOnHand.Visible = True
    End Sub
    Sam I am (as well as Confused at times).

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    that works thanx. dunno why i didn't try that next will be:

    1.) add / delete record
    2.) edit name - (incase it's misspelled) & edit required and onhand


    this app will really help me vs paper and pen or excell

  16. #16
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,500

    Re: multiple record info from access DB

    To add a record, you will be using an "Insert" query....look up examples of this.
    Make an attempt and post your code.

    Deleting records are very simple...use a "Delete" query. Look for examples.

    To Edit, you will want to use a Select query (like already demonstrated), put the result into a text box, and then use an "Update" query to put any changed name back into the database.

    Take one of these challenges at a time...post each attempt with 'errors' if it fails.

    Sammi
    Sam I am (as well as Confused at times).

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    Quote Originally Posted by SamOscarBrown View Post
    To add a record, you will be using an "Insert" query....look up examples of this.
    Make an attempt and post your code.

    Deleting records are very simple...use a "Delete" query. Look for examples.

    To Edit, you will want to use a Select query (like already demonstrated), put the result into a text box, and then use an "Update" query to put any changed name back into the database.

    Take one of these challenges at a time...post each attempt with 'errors' if it fails.

    Sammi
    everything i search for adding an entry is either vbNET or vba or not in english... but then again, i only search youtube vids..
    Last edited by BlakeSheldon; Aug 31st, 2024 at 01:17 PM.

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    updated app.. i haven't gotten to writing to or updating the the db yet. but go to the top menu and select seeds and then vegetables..
    Last edited by BlakeSheldon; Sep 2nd, 2024 at 08:18 PM.

  19. #19
    Hyperactive Member
    Join Date
    Jul 2022
    Posts
    362

    Re: multiple record info from access DB

    I added some code to the Add Item (Form2) as well as a little clean up (nothing major)

    You would use the same style code that is in the add item button on form2 except you will use an Update statement
    Attached Files Attached Files

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    ok, thanx. The issue still is form 3 i can't get past that error to make the form work

  21. #21
    Hyperactive Member
    Join Date
    Jul 2022
    Posts
    362

    Re: multiple record info from access DB

    Okay, here. You just needed to name the textboxes to match the file contents.
    Attached Files Attached Files

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    i went to it's properties and chose the imagelist1 and it still says:

    Set ListView1.Icons = ImageList1
    Last edited by BlakeSheldon; Sep 3rd, 2024 at 02:24 PM. Reason: spelling

  23. #23
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,500

    Re: multiple record info from access DB

    So what does that mean to you?

    Do you even know HOW to initialize your imagelist?
    Sam I am (as well as Confused at times).

  24. #24

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    Quote Originally Posted by SamOscarBrown View Post
    So what does that mean to you?

    Do you even know HOW to initialize your imagelist?
    apparently not.. but i did point out the imagelist1 in the listview1 - if that isn't it then no i don't know cause it's not working and i'm not understanding why

    and this link doesn't help.. as i just stated
    https://www.vbforums.com/showthread....alize-Resolved
    Last edited by BlakeSheldon; Sep 3rd, 2024 at 03:28 PM.

  25. #25

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    ok, i give.. what am i not getting?

  26. #26
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,736

    Re: multiple record info from access DB

    Quote Originally Posted by BlakeSheldon View Post
    ok, i give.. what am i not getting?
    ImageList1.ListImages.Add ?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  27. #27
    Hyperactive Member
    Join Date
    Jul 2022
    Posts
    362

    Re: multiple record info from access DB

    It worked for me, using the code for form3

    edit: add screenshot
    Attached Images Attached Images  

  28. #28
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,500

    Re: multiple record info from access DB

    @jd....you obviously have the images in a path on your computer, which OP does not...hence, he can't populate his imagelist without it.

    If you want to help him, put those images in a sub directory below app.path and then change your code to populate the imagelist from THAT folder.
    Sam I am (as well as Confused at times).

  29. #29

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    Quote Originally Posted by Zvoni View Post
    ImageList1.ListImages.Add ?
    now it says: required argument is required

    Code:
    Set ListView1.Icons = ImageList1.ListImages.Add

  30. #30
    Hyperactive Member
    Join Date
    Jul 2022
    Posts
    362

    Re: multiple record info from access DB

    OMG sorry geesh, don't get old! Just put any images and name them what you want. I'm not where I can get those files at the moment. I got I'll check my browsing history for where I got them.

    Edit: on my phone, the swipe keyboard giving me fits.

    I found the images on my PC. Look for arcade.jpg, I'll look on my laptop in the morning.

    Sorry, sometimes I amaze myself with how dumb I can be.
    Last edited by jdelano; Sep 4th, 2024 at 01:24 PM.

  31. #31

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    yeah, i can get the form and images to work from the temp folder in C folder.. but not an imagelist. but this will do, just gotta change the c\temp to app.path & "images" or something along those lines.

    even then, i still to change that a little more because the form menus have sub menus.. so if say i click on "vegetables" and then say i click a sub menu.. say "peppers", then the imagelist would show the pepper images. if say i clicked con cucumbers, then the imagelist would show the images of cucumbers.

    and then there is the count thing.. one thing at a time i guess
    Last edited by BlakeSheldon; Sep 4th, 2024 at 01:38 PM.

  32. #32
    Hyperactive Member
    Join Date
    Jul 2022
    Posts
    362

    Re: multiple record info from access DB

    Okay, I'll be on in the early morning (EDT) so dm me whatever you need help with.

  33. #33

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    i may have a new twist on this.. i'm working on a new form.. tho i do want to keep this form because it has it's uses.. but not for seeds.. same concept tho.. we'll talk.. still designing atm.. very similar, just opposite/different... again, each one has it's purpose.. i can explain the differences later if needed
    Last edited by BlakeSheldon; Sep 4th, 2024 at 05:52 PM.

  34. #34
    Hyperactive Member
    Join Date
    Jul 2022
    Posts
    362

    Re: multiple record info from access DB

    I see you have a bunch of flags in the imagelist, you can preload them like you have or keep using the SetupImagelist I switched it to use the images folder and also added code to use the temp subfolder to look for the text files.

    The code reads whatever jpgs that are in that folder to display the image and simply uses the filename (before .jpg) as the key, the key is used to open an associated text file, which is just the jpg filename with a .txt, to get the data.

    I did this simply to show how to load the image list and display a bit of something when it was clicked. You can use an alternative method to save and display your data.

    Code:
    Dim imagesFilePath As String
    
    Private Sub Form_Load()
        
        ' set the images path to a subfolder of where the app is
        imagesFilePath = App.Path & "\images\"
        
        SetupImageList
        FillListView
        ListView1_Click ' select the first item in the list
         
    End Sub
    
    Private Sub SetupImageList()
    
        ' find the JPGs in the folder and add them to the imagelist for the listview
        Dim jpgFiles As String
        Dim jpgFileName As String
            
        jpgFiles = Dir(imagesFilePath & "*.jpg")
        
        ImageList1.ListImages.Clear
            
        On Error Resume Next
        While jpgFiles > vbNullString
                    
            ' extract the filename to the left of the period
            jpgFileName = Left(jpgFiles, InStr(jpgFiles, ".") - 1)
                    
            ' add the image to the imagelist control
            ImageList1.ListImages.Add , jpgFileName, LoadPicture(imagesFilePath & jpgFiles)
            
            jpgFiles = Dir
        Wend
        
    End Sub
    
    Private Sub FillListView()
    
        ' assign the image to the filename
        Dim jpgFiles As String
        Dim jpgFileName As String
        
        jpgFiles = Dir(imagesFilePath & "*.jpg")
        ListView1.ListItems.Clear
        Set ListView1.Icons = ImageList1
        
        On Error Resume Next
        While jpgFiles > vbNullString
            
            ' extract the filename to the left of the period
            jpgFileName = Left(jpgFiles, InStr(jpgFiles, ".") - 1)
            
            ' add it to the listview with its assigned image
            ListView1.ListItems.Add , , jpgFileName, jpgFileName
            
            If Err <> 0 Then
                MsgBox jpgFileName & " does not have an associated image", vbInformation, "Skipping"
            
            End If
            
            jpgFiles = Dir
        Wend
        
    End Sub
    
    Private Sub ListView1_Click()
        
        Dim fName As String
        Dim fso As Scripting.FileSystemObject
        Dim fl As TextStream
        Dim lineData() As String
           
        Set fso = New Scripting.FileSystemObject
        
        'fName = imagesFilePath & ListView1.SelectedItem.Text & ".txt"
        
        ' use the temp subfolder to look for the associated text file
        fName = App.Path & "\Temp\" & ListView1.SelectedItem.Text & ".txt"
        
        If fso.FileExists(fName) Then
            ' there is a file to read
            Set fl = fso.OpenTextFile(fName)
            
            ' read the file, match the left portion of :
            ' to write to the textbox of the same name
            Do Until fl.AtEndOfStream
                lineData = Split(fl.ReadLine, ":")
                Me.Controls("txt" & lineData(0)).Text = lineData(1)
            
            Loop
            
            fl.Close
            Set fl = Nothing
            
        Else
            MsgBox "There isn't an associated information file (" & fName & ") for the selected item"
        End If
        
        Set fso = Nothing
    End Sub
    Attached Images Attached Images  
    Attached Files Attached Files

  35. #35

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    sorry, been working 2 off shifts and spent the day cooking and canning. i'll look at it tomorrow as really need to get some well deserved sleep

  36. #36
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,500

    Re: multiple record info from access DB

    This OP sounds familiar. Hmmmmm.
    Sam I am (as well as Confused at times).

  37. #37

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    k, so i added an imagelist with progress bar images to the form.. however, i do not know how to code the percentage to work with what is required and what's on hand..


    k, so..

    if 0 OR 1 on hand is less than the qty required then show imagelist 1 picture
    if 2 to 4 then show imagelist 2 picture
    if half on hand vs what is required show imagelist 3 picture
    if whatever to whatever on hand vs required show imagelist 4 picture
    if on hand is equal to or great than required then show imagelist 5 picture..

    something like that - i hope that makes sense..

    i know it will be a select case.. but i only did this to test an IF statement which i knew would work.. but it's NOT percentage code.. so i couldn't try 1/2 percentage or any other percentage..
    Code:
            If lblOnHand = lblRequired Then
            Image1.Picture = ImageList1.ListImages(5).Picture
            End If
    Last edited by BlakeSheldon; Sep 6th, 2024 at 08:10 PM.

  38. #38

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    i know that pic shows 6 out of 6 and should have the full bar image.. again just for demonstration.. the images are in the imagelist on the form
    Last edited by BlakeSheldon; Sep 6th, 2024 at 07:48 PM.

  39. #39
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,341

    Re: multiple record info from access DB

    The problem with hard coding values like "If it is 2 to 4 on hand then show picture 2" is that, what if the item on hand is something where your required number of them is only 4 to begin with?

    The better option, assuming that the images are in proper numerical order in the ImageList is something like this (freehanded, not tested, and not guaranteed to work perfectly for you without some potentially minor changes):

    Code:
    If cint(lblOnHand.Caption) < cint(lblRequired.Caption) Then
      Image1.Picture = ImageList1.ListImages(int(5*cint(lblOnHand.Caption) / cint(lblRequired.Caption))).Picture
    Else
      Image1.Picture = ImageList1.ListImages(5).Picture
    End If

  40. #40

    Thread Starter
    Lively Member
    Join Date
    Aug 2024
    Posts
    122

    Re: multiple record info from access DB

    well if it's 4 & 4 as required and on hand then it would show image 5 a full bar. so it would have to be like 2 or 3 to show image 4 as example for talk sake i know that is NOT a percentage.. i'm just looking at limit between req and on hand.. but i can try yer idea.. like i said, i do not know how to do percentages between REQ and ONHAND.. there has to be a limit for each image..

    100%, 75%, 50%, 25% & then anything under 25% - that would be the 5 images
    Last edited by BlakeSheldon; Sep 6th, 2024 at 08:33 PM.

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