Results 1 to 16 of 16

Thread: [RESOLVED] Loading data into Combox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Resolved [RESOLVED] Loading data into Combox

    My Code:

    Code:
    Private Sub Form_Load()
    Set cn = New ADODB.Connection
    cn.ConnectionString = "Provider= Microsoft.JET.OLEDB.3.51;" & "Data Source=" & App.Path & " \Index.mdb"
      cn.Open
    
      Set rsIndex = New ADODB.Recordset
    
    
      rsIndex.Open " select Ime,Prezime from StudentDetalji order by Ime",cn,adOpendynamic,adLockOptimistic
    
      Do until rsIndex.EOF
      Combo1.AddItem rsIdex("Ime")
      Combo1.AddItem rsIndex("Prezime")
      rsIndex.MoveNext
    Loop
    What is does: Takes data from field1(Ime) places it into the combobox,then takes data from field2(Prezime) and places it under fileld1.I would it to place that data after field1.
    So I would have:
    Field1 Filed2

    NOT
    Filed1
    Field2

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Loading data into Combox

    Try
    Code:
    Combo1.AddItem rsIdex("Ime") & vbTab & rsIndex("Prezime")
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: Loading data into Combox

    Yeah,that works.But now,what if I want to delete the selected item?
    Would this be the correct syntax?

    rsIndex.open " Delete from TableName Where FieldName1= ' " & Combo1.Text & " ' "

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: Loading data into Combox

    Yes,this also works,as long as I load only ONE field from DB to ComboBox.
    But,if I load TWO fields then I cant say
    FieldName1= ' " & Combo1.Text & " ' " AND FieldName2 = ' " & Combo1.Text & " ' "

    Help!

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Loading data into Combox

    What are the values for IME?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: Loading data into Combox

    Both IME and PREZIME are text values.To translate into English IME=first name,PREZIME=last name

    So I need to check if the first part of the combobox item = IME, and the second part = PREZIME, if so DELETE it from the database,refresh the listview.

    Or I could just merge those two fields in the DB,and call the FULLNAME,but then I would have to reorganise my entire program.

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Loading data into Combox

    here is an alternative...

    Change Dee's code in post 2 to

    '-- Instead of Tab use "#" as firstname or lastname might have a space between them
    StrName = rsIdex("Ime") & "#" & rsIndex("Prezime")
    Combo1.AddItem = StrName

    Later you can use split to get the first name and last name which you can use to delete the data in the table for example

    vb Code:
    1. Dim StrName As String, MyArray() As String
    2. '
    3. 'Your other code
    4. '
    5. '-- Instead of Tab use "#"
    6. StrName = rsIdex("Ime") & "#" & rsIndex("Prezime")
    7. Combo1.AddItem = StrName
    8. '
    9. 'Your other code
    10. '
    11. MyArray = Split(StrName, "#")
    12.  
    13. Trim (MyArray(0)) '-- This will give you the first name
    14. Trim (MyArray(1)) '-- This will give you the last name
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: Loading data into Combox

    I followed you example,but it doesnt work in my code.

    Code:
    Do Until reIndex.EOF
    strIme = rsIndex("Ime") & "#" & rsIndex("Prezime")
    Combo1.AddItem = strIme
    rsIndex.MoveNext
    Loop
    
       MyArray = Split(strIme, "#")
       Trim (MyArray(0))
       Trim (MyArray(1))
       
       end sub

    That part is under FormLoad.I open a connection with the DB before that,and select fields,as shown in my post num.1.

    So the error is ARGUMENT NOT OPTIONAL
    And .AddItem is highlited

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Loading data into Combox

    Combo1.AddItem = strIme

    my apologies. it should be

    Combo1.AddItem strIme
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: Loading data into Combox

    Yeah,I noticed that too.Fixed it.
    Is this the correct syntax to delete from DB?

    Code:
    rsIndex.open "Delete from StudentDetalji Where Ime= 'MyArray(0)' AND Prezime= 'MyArray(1)' "
    If I run it like that,it says Subscript out of range

    And I read that an array subscript is not valid because it falls outside the allowable range.

    Trim (MyArray(0)) is highlited.

  11. #11
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Loading data into Combox

    try storing MyArray(0) and MyArray(1) in a variable, say something like this (doing this from memory... so ignore typos if any )

    Temp1= MyArray(0)
    Temp2=MyArray(1)
    rsIndex.open "Delete from StudentDetalji Where Ime= '" & Temp1 & "' AND Prezime= '" & Temp2 & "'"
    Last edited by Siddharth Rout; Feb 13th, 2009 at 11:47 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: Loading data into Combox

    I have tried that too,but still doesnt work.Again the same error.
    I am really out of ideas.

  13. #13
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Loading data into Combox

    post your complete code...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: Loading data into Combox

    Code:
    Private Sub Form_Load()
    Dim strIme as String
    Dim cn as Connection
    Dim rsIndex as Recordset
    
    Set cn = New ADODB.Connection
    cn.ConnectionString = "Provider= Microsoft.JET.OLEDB.3.51;" & "Data Source=" & App.Path & " \Index.mdb"
    
      cn.Open
    
      Set rsIndex = New ADODB.Recordset
    
    
      rsIndex.Open " select Ime,Prezime from StudentDetalji order by Ime",cn,adOpendynamic,adLockOptimistic
    
      Do until rsIndex.EOF
      strIme = rsIndex("Ime") & " " & rsIndex("Prezime")
      Combo1.AddItem strIme
      rsIndex.MoveNext
    Loop
    
     end sub
    Code:
    Private sub cmdBrisanjeOK_Click()
    Dim MyArray as String, x1 as String, x2 as String
    
    MyArray = Split (strIme, " ")
    x1 = MyArray(0)
    x2 = MyArray(1)
    
    rsIndex.open "Delete from StudentDetalji where Ime = '" & x1 & "' AND Prezime= '" x2 & "'", cn, adOpenDynamic,adLockOptimistic
    cn.Close
    
    Unload Me
    frmIndex.Show
    
    End Sub
    I'm not sure if I'm wrong here or not,but I opened the connection with DB only once,in form load.Do I have to open it again in Button Click,or is it OK to leave it like that?
    It works fine until I click the button to delete the selected item in combobox.

  15. #15
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Loading data into Combox

    Okay I have cleaned your code and my comments are in green. You will now understand why the errors were happening...

    Hope this helps...

    vb Code:
    1. '-- You had incorrectly declared the array
    2. Dim MyArray() As String, x1 As String, x2 As String
    3.  
    4. Private Sub Form_Load()
    5.     Dim cn As New ADODB.Connection, rsIndex As New ADODB.Recordset
    6.     Dim DBFullName As String, strQuery As String, strIme As String
    7.    
    8.     '-- Not the best way but a very clean way of handling your code
    9.     DBFullName = App.Path & " \Index.mdb"
    10.     cn.ConnectionString = "Provider= Microsoft.JET.OLEDB.3.51;" & "Data Source=" & DBFullName
    11.     cn.open
    12.     strQuery = "Select Ime,Prezime from StudentDetalji Order by Ime"
    13.    
    14.     rsIndex.open strQuery, cn, adOpenDynamic, adLockOptimistic
    15.  
    16.     Do Until rsIndex.EOF
    17.         '-- Like I suggested do not use space as full names
    18.         '-- can have more than once space
    19.         strIme = rsIndex("Ime") & "#" & rsIndex("Prezime")
    20.         Combo1.AddItem strIme
    21.         rsIndex.MoveNext
    22.     Loop
    23.    
    24.     '-- It is good to close the connection instead of keeping it open
    25.     '-- unnecessarily
    26.     rsIndex.Close
    27.     Set rsIndex = Nothing
    28.    
    29.     cn.Close
    30.     Set cn = Nothing
    31. End Sub
    32. Private Sub cmdBrisanjeOK_Click()
    33.     'MyArray = Split(strIme, "#") '--- Where is the value of strIme coming from????? and hence the error
    34.     'if you are picking the value from combo then try something like this in lieu of the above
    35.     MyArray = Split(Trim(Combo1.Text), "#")
    36.    
    37.     x1 = Trim(MyArray(0))
    38.     x2 = Trim(MyArray(1))
    39.    
    40.     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    41.     '-- Open the connection once again here before performing the below action as shown above
    42.     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    43.    
    44.     rsIndex.open "Delete from StudentDetalji where Ime = '" & x1 & "' AND Prezime= '" _
    45.     & x2 & "'", cn, adOpenDynamic, adLockOptimistic
    46.    
    47.     '''''''''''''''''''''''''''''''''''''
    48.     '-- Close connecton here
    49.     '''''''''''''''''''''''''''''''''''''
    50.    
    51.     Unload Me
    52.    
    53.     frmIndex.Show
    54. End Sub
    Last edited by Siddharth Rout; Feb 13th, 2009 at 01:40 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    313

    Re: Loading data into Combox

    Thank you very much,the code you wrote is excellent and works like a charm!

    I would like to ask you a question.You seem to know very much when it comes to VB.How did you learn it so well?Tutorials?Forums like this one?Books?
    I'm a real begginer in this and I know I lack the basics to become a bit more advanced.

    Once again thank you!

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