|
-
Feb 12th, 2009, 12:38 PM
#1
Thread Starter
Hyperactive Member
[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
-
Feb 12th, 2009, 12:54 PM
#2
Re: Loading data into Combox
Try
Code:
Combo1.AddItem rsIdex("Ime") & vbTab & rsIndex("Prezime")
-
Feb 12th, 2009, 02:35 PM
#3
Thread Starter
Hyperactive Member
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 & " ' "
-
Feb 12th, 2009, 02:50 PM
#4
Thread Starter
Hyperactive Member
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!
-
Feb 12th, 2009, 03:08 PM
#5
Re: Loading data into Combox
What are the values for IME?
-
Feb 12th, 2009, 03:15 PM
#6
Thread Starter
Hyperactive Member
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.
-
Feb 12th, 2009, 08:48 PM
#7
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:
Dim StrName As String, MyArray() As String
'
'Your other code
'
'-- Instead of Tab use "#"
StrName = rsIdex("Ime") & "#" & rsIndex("Prezime")
Combo1.AddItem = StrName
'
'Your other code
'
MyArray = Split(StrName, "#")
Trim (MyArray(0)) '-- This will give you the first name
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
-
Feb 13th, 2009, 05:52 AM
#8
Thread Starter
Hyperactive Member
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
-
Feb 13th, 2009, 06:14 AM
#9
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
-
Feb 13th, 2009, 11:15 AM
#10
Thread Starter
Hyperactive Member
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.
-
Feb 13th, 2009, 11:43 AM
#11
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
-
Feb 13th, 2009, 12:08 PM
#12
Thread Starter
Hyperactive Member
Re: Loading data into Combox
I have tried that too,but still doesnt work.Again the same error.
I am really out of ideas.
-
Feb 13th, 2009, 12:17 PM
#13
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
-
Feb 13th, 2009, 01:00 PM
#14
Thread Starter
Hyperactive Member
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.
-
Feb 13th, 2009, 01:27 PM
#15
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:
'-- You had incorrectly declared the array
Dim MyArray() As String, x1 As String, x2 As String
Private Sub Form_Load()
Dim cn As New ADODB.Connection, rsIndex As New ADODB.Recordset
Dim DBFullName As String, strQuery As String, strIme As String
'-- Not the best way but a very clean way of handling your code
DBFullName = App.Path & " \Index.mdb"
cn.ConnectionString = "Provider= Microsoft.JET.OLEDB.3.51;" & "Data Source=" & DBFullName
cn.open
strQuery = "Select Ime,Prezime from StudentDetalji Order by Ime"
rsIndex.open strQuery, cn, adOpenDynamic, adLockOptimistic
Do Until rsIndex.EOF
'-- Like I suggested do not use space as full names
'-- can have more than once space
strIme = rsIndex("Ime") & "#" & rsIndex("Prezime")
Combo1.AddItem strIme
rsIndex.MoveNext
Loop
'-- It is good to close the connection instead of keeping it open
'-- unnecessarily
rsIndex.Close
Set rsIndex = Nothing
cn.Close
Set cn = Nothing
End Sub
Private Sub cmdBrisanjeOK_Click()
'MyArray = Split(strIme, "#") '--- Where is the value of strIme coming from????? and hence the error
'if you are picking the value from combo then try something like this in lieu of the above
MyArray = Split(Trim(Combo1.Text), "#")
x1 = Trim(MyArray(0))
x2 = Trim(MyArray(1))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'-- Open the connection once again here before performing the below action as shown above
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
rsIndex.open "Delete from StudentDetalji where Ime = '" & x1 & "' AND Prezime= '" _
& x2 & "'", cn, adOpenDynamic, adLockOptimistic
'''''''''''''''''''''''''''''''''''''
'-- Close connecton here
'''''''''''''''''''''''''''''''''''''
Unload Me
frmIndex.Show
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
-
Feb 13th, 2009, 02:14 PM
#16
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|