buton sorguyu combobox olarak değiştirmek

Katılım
30 Ocak 2008
Mesajlar
154
Excel Vers. ve Dili
offis 2003
arkadaşlar bir örnek buldum bunu kendi uygulamama göre düzenlemek istiyorum sizden istediğim küücük bi düzeltme yapamadım asagıda verdiğim koddan acces veritabanlı 4 adet command buton var access de 4 adet tablo var Tablo1 -2-3-4 seklinde userformda CommandButon1 e tıkladıgımda tablo1 in içindekileri listbox da listeliyor yapmak istediğim combobox olucak buton1 -2-3-4 comboboxdan hangisini secercem o tablo içindekiler listbox da cıksın ?
Public Baglanti As ADODB.Connection
Public KayitSeti As ADODB.Recordset
Public BagMetin As String, Sorgu As String
Public Veri As Variant
Public Sutun As Long

Private Sub ComboBox1_Change()
Set Baglanti = New ADODB.Connection

BagMetin = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " _
& Application.ThisWorkbook.Path & "\" & "VT1.mdb;Persist Security Info=False"

Sorgu = "SELECT * FROM Tablo1 ORDER BY Kimlik"

With Baglanti
.CursorLocation = adUseClient
.Open BagMetin
Set KayitSeti = .Execute(Sorgu)
End With

With KayitSeti
Set .ActiveConnection = Nothing
Sutun = .Fields.Count
Veri = .GetRows
End With

Baglanti.Close

With UserForm1
With .ListBox1
.Clear
.ColumnCount = Sutun
.BoundColumn = Sutun
.List = Application.Transpose(Veri)
.ListIndex = -1
End With
End With

Set KayitSeti = Nothing
Set Baglanti = Nothing
End Sub

Private Sub CommandButton1_Click()
Set Baglanti = New ADODB.Connection

BagMetin = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " _
& Application.ThisWorkbook.Path & "\" & "VT1.mdb;Persist Security Info=False"

Sorgu = "SELECT * FROM Tablo1 ORDER BY Kimlik"

With Baglanti
.CursorLocation = adUseClient
.Open BagMetin
Set KayitSeti = .Execute(Sorgu)
End With

With KayitSeti
Set .ActiveConnection = Nothing
Sutun = .Fields.Count
Veri = .GetRows
End With

Baglanti.Close

With UserForm1
With .ListBox1
.Clear
.ColumnCount = Sutun
.BoundColumn = Sutun
.List = Application.Transpose(Veri)
.ListIndex = -1
End With
End With

Set KayitSeti = Nothing
Set Baglanti = Nothing
End Sub

Private Sub CommandButton2_Click()
Set Baglanti = New ADODB.Connection

BagMetin = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " _
& Application.ThisWorkbook.Path & "\" & "VT1.mdb;Persist Security Info=False"

Sorgu = "SELECT * FROM Tablo2 ORDER BY Kimlik"

With Baglanti
.CursorLocation = adUseClient
.Open BagMetin
Set KayitSeti = .Execute(Sorgu)
End With

With KayitSeti
Set .ActiveConnection = Nothing
Sutun = .Fields.Count
Veri = .GetRows
End With

Baglanti.Close

With UserForm1
With .ListBox1
.Clear
.ColumnCount = Sutun
.BoundColumn = Sutun
.List = Application.Transpose(Veri)
.ListIndex = -1
End With
End With

Set KayitSeti = Nothing
Set Baglanti = Nothing
End Sub

Private Sub CommandButton3_Click()
Set Baglanti = New ADODB.Connection

BagMetin = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " _
& Application.ThisWorkbook.Path & "\" & "VT1.mdb;Persist Security Info=False"

Sorgu = "SELECT * FROM Tablo3 ORDER BY Kimlik"

With Baglanti
.CursorLocation = adUseClient
.Open BagMetin
Set KayitSeti = .Execute(Sorgu)
End With

With KayitSeti
Set .ActiveConnection = Nothing
Sutun = .Fields.Count
Veri = .GetRows
End With

Baglanti.Close

With UserForm1
With .ListBox1
.Clear
.ColumnCount = Sutun
.BoundColumn = Sutun
.List = Application.Transpose(Veri)
.ListIndex = -1
End With
End With

Set KayitSeti = Nothing
Set Baglanti = Nothing
End Sub

Private Sub CommandButton4_Click()
Set Baglanti = New ADODB.Connection

BagMetin = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " _
& Application.ThisWorkbook.Path & "\" & "VT1.mdb;Persist Security Info=False"

Sorgu = "SELECT * FROM Tablo4 ORDER BY Kimlik"

With Baglanti
.CursorLocation = adUseClient
.Open BagMetin
Set KayitSeti = .Execute(Sorgu)
End With

With KayitSeti
Set .ActiveConnection = Nothing
Sutun = .Fields.Count
Veri = .GetRows
End With

Baglanti.Close

With UserForm1
With .ListBox1
.Clear
.ColumnCount = Sutun
.BoundColumn = Sutun
.List = Application.Transpose(Veri)
.ListIndex = -1
End With
End With

Set KayitSeti = Nothing
Set Baglanti = Nothing
End Sub

Private Sub UserForm_Click()

End Sub
 
Katılım
25 Aralık 2005
Mesajlar
4,160
Excel Vers. ve Dili
MS Office 2010 Pro Türkçe
Sayın dreamymost,

Gerçi kodlarınız Excel formu için. Açılan kutunun tek sütunu ve tablo adlarını içeriyorsa:

Kod:
Private Sub ComboBox1_Change()
Dim strTabloAdi As String
strTabloAdi= ComboBox1.Value

Set Baglanti = New ADODB.Connection

BagMetin = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " _
& Application.ThisWorkbook.Path & "\" & "VT1.mdb;Persist Security Info=False"

Sorgu = "SELECT * FROM " & strTabloAdi & " ORDER BY Kimlik"

With Baglanti
    .CursorLocation = adUseClient
    .Open BagMetin
    Set KayitSeti = .Execute(Sorgu)
End With
     
With KayitSeti
    Set .ActiveConnection = Nothing
    Sutun = .Fields.Count
    Veri = .GetRows
End With
    
    Baglanti.Close
    
With UserForm1
    With .ListBox1
        .Clear
        .ColumnCount = Sutun
        .BoundColumn = Sutun
        .List = Application.Transpose(Veri)
        .ListIndex = -1
    End With
End With

Set KayitSeti = Nothing
Set Baglanti = Nothing
End Sub
Ama programlama yaparken değişkenlere veri türlerinin belirtilmesi iyi bir alışkanlıktır. C gibi dillerde mutlaka belirtmeniz gerekir.

İyi çalışmalar
 
Üst