CariDotMy

 Forgot password?
 Register

ADVERTISEMENT

View: 2673|Reply: 16

Combo Box

[Copy link]
Post time 15-10-2006 12:01 AM | Show all posts |Read mode
Hello, saya gune vee bee 6. Saya nak tau macam mana nak gune combo box pastuh ape yg kite pilih akan kluar kat textbox?
Sebagai contoh, byk2 nama makanan dalam list kat combo box and bile kite click salah satu nama makanan, nama tu akan appear kat textbox. Minta tolong ajar ye. ;D
Reply

Use magic Report


ADVERTISEMENT


Post time 15-10-2006 12:28 AM | Show all posts
Consider this example.

Put one combo box and a text box in a form. Name the combo box Combo1 and the text box Text1.

Lepas tu, in the Form Load event, add the following:

Sub Form_Load
   Combo1.Clear ' clears the combo box
   Combo1.AddItem "Ayam goreng"
   Combo1.AddItem "Daging masak merah"
   Combo1.AddItem "Bistik ayam"
   Combo1.AddItem "Kerabu mangga"
End Sub

Lepas tu, dalam event Click untuk Combo1, add the following:
(ataupun boleh guna event AfterUpdate / Change, saya tak pasti yg mana paling sesuai)

Sub Combo1_Click
  Text1 = Combo1
End Sub

Pastu run form tu, jalan ke idak.

Semoga berjaya.
Reply

Use magic Report

 Author| Post time 15-10-2006 01:26 AM | Show all posts
Ouhh terima kasih byk2 shahnazz tapi saya amik data dari microsoft access, itu la masalah nye.
Reply

Use magic Report

 Author| Post time 15-10-2006 01:31 AM | Show all posts
Saya masih tak tau mcm mana nak listkan data yg ada kat access gune combobox pastuh bile clcik kluar kat textbox.
Reply

Use magic Report

Post time 15-10-2006 02:00 AM | Show all posts
wow, fast reply.

sama ada update List property kat combo box, atau programmatically use the AddItem method of the combo box.

untuk retrieve from access table, i can give you a simple script using ADO. call this subroutine (in the Form_Load event maybe) to update the list in your combo box.


  1. Sub UpdateComboBox()

  2.    ' using ADO connection
  3.    Dim cn As New ADODB.Connection
  4.    cn.ConnectionString = "... what is your connection string? ..."
  5.    cn.Open

  6.    ' clear the list
  7.    Combo1.Clear
  8.   
  9.    'get values from table and populate the combo box
  10.    Dim rs as new ADODB.Recordset
  11.    rs.ActiveConnection = cn
  12.    rs.Open "select nama_makanan from makanan"
  13.    while not rs.eof
  14.      Combo1.AddItem rs("nama_makanan")
  15.      rs.movenext
  16.    rs.close

  17. End Sub
Copy the Code


i think that should work. sorry tak larat nak verify OK ke idak, mengantuk sangat nih, hehe...
Reply

Use magic Report

 Author| Post time 15-10-2006 12:25 PM | Show all posts
Hmm kite dah try tapi takde apa terjadi pon. Camne ek?
Reply

Use magic Report

Follow Us
Post time 15-10-2006 11:15 PM | Show all posts
cuba paste your code kat sini
Reply

Use magic Report

 Author| Post time 16-10-2006 08:43 PM | Show all posts
Private Sub UpdateComboBox()

   ' using ADO connection
   Dim cn As New ADODB.Connection
   cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=
   cn.Open

   ' clear the list
   Combo1.Clear
  
   'get values from table and populate the combo box
   Dim rs As New ADODB.Recordset
   rs.ActiveConnection = cn
   rs.Open "select Subject Code from Subjects"
   While Not rs.EOF
     Combo1.AddItem rs("Subject Code")
     rs.MoveNext
   rs.Close

End Sub

[ Last edited by  Bremen at 16-10-2006 09:11 PM ]
Reply

Use magic Report


ADVERTISEMENT


Post time 16-10-2006 09:31 PM | Show all posts
Private Sub UpdateComboBox()

   ' using ADO connection
   Dim cn As New ADODB.Connection
   cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=
   cn.Open

   ' clear the list
   Combo1.Clear
  
   'get values from table and populate the combo box
   Dim rs As New ADODB.Recordset
   rs.ActiveConnection = cn
   rs.Open "select Subject Code from Subjects"
   While Not rs.EOF
     Combo1.AddItem rs("Subject Code")
     debug.print "Subject code: " & rs("subject code")
     rs.MoveNext
   rs.Close

End Sub

Dalam Microsoft Access (atau apa-apa database pun), cuba elakkan guna nama field yang ada spaces.
Contoh yang sesuai:
- SubjectCode
- subject_code

Contoh yang tak sesuai:
- subject code (ambiguous and actually syntactically not correct for SQL statements)

The workaround for microsoft access is to put square braces on columns in SQL statements, macam ni:
Select [Subject Code] From Subjects (try amending that to your code and see if it works). But kaedah ni hanya berkesan pada Microsoft Access sahaja.

Just as a test, add the code in green colour to your existing coding, run, pastu tengok output / immediate window, ada tak any values keluar. Kalau tak, kena olah semula database.

Otherwise, logically your algorithm is OK. I assume you call the UpdateComboBox sub in the Form_Load event.
Reply

Use magic Report

 Author| Post time 16-10-2006 10:21 PM | Show all posts
Coding ni taruk kat form load atau combo box?
Reply

Use magic Report

 Author| Post time 16-10-2006 10:33 PM | Show all posts
dah dapat tapi combo box tuh cuma listkan 1st row saje, yg row lain dalam database tu takde plak dia listkan.
Reply

Use magic Report

Post time 17-10-2006 10:10 AM | Show all posts
While Not rs.EOF
     Combo1.AddItem rs("Subject Code")
     debug.print "Subject code: " & rs("subject code")
     rs.MoveNext
   Wend
   rs.Close

tambah satu lagi line of code sepertimana yang ditunjukkan.
sorry tak perasan...

selamat mencuba
Reply

Use magic Report

Post time 17-10-2006 10:53 PM | Show all posts
tumpang tanya...
korang tau tak website yg ada contoh2 projek vb nih... aku nk wat rujukan la...
Reply

Use magic Report

 Author| Post time 20-10-2006 10:44 PM | Show all posts
Macam mana saya nak upload gambar dlm ni? Saya ade bende nak tanye.
Reply

Use magic Report

Post time 21-10-2006 11:49 AM | Show all posts
Originally posted by Bremen at 20-10-06 10:44 PM
Macam mana saya nak upload gambar dlm ni? Saya ade bende nak tanye.


upload gambar kat image shack
n watkan image tu thumbnail so xde la bsar n  lama nak loading
Reply

Use magic Report

 Author| Post time 29-10-2006 09:14 AM | Show all posts

Listview dan datacombo

Hello semua, saya nak tau camne nak listkan kat listview  ape yg saya pilih dari datacombo di atas? saya tak tau apa2 coding...kurang paham. Mintak tolong sape2 please :D

Reply

Use magic Report


ADVERTISEMENT


kayos_rayos This user has been deleted
Post time 29-12-2006 10:25 AM | Show all posts
Click kat Combo box
Private Sub Combobox1_Click()
''' letak kod kat sini...
End Sub

Application Developer
IT.BaseLogger
0123959774
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

 

ADVERTISEMENT


Forum Hot Topic

 

ADVERTISEMENT


 


ADVERTISEMENT
Follow Us

ADVERTISEMENT


Mobile|Archiver|Mobile*default|About Us|CariDotMy

28-12-2024 10:36 PM GMT+8 , Processed in 0.064150 second(s), 32 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

Quick Reply To Top Return to the list