Avatar of egovernment
egovernmentFlag for Afghanistan

asked on 

Fill a combobox from SQL Server

I use this code is working only with Acceass database not SQL Server database what the reason ?

    vr_RdSet.Open "SELECT [XXX] FROM [YYY] WHERE [XXX] IS NOT NULL " & _
    "ORDER BY [XXX]", vr_Conn, adOpenStatic, adLockReadOnly
    If Not vr_RdSet.EOF Then
        With Combobox278
            Set .Recordset = vr_RdSet
            .Requery
        End With
    End If
    vr_RdSet.Close
    Set vr_RdSet = Nothing

Open in new window


I'm using unbound form + Vba + Ado + Sql server
Microsoft AccessMicrosoft SQL Server 2008Visual Basic Classic

Avatar of undefined
Last Comment
nike_golf
Avatar of danishani
danishani
Flag of United States of America image

I am not sure you have posted all your code, but below an example on how to do this. You might change the ConnectionString accordingly.

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql As String
 
  Set cn = New ADODB.Connection
  cn.ConnectionString = "Provider=SQLNCLI;Server=USJILLMA\SQLEXPRESS;" & _
    "Database=faa;Trusted_Connection=yes;"
  cn.Open
 
  sql ="SELECT [XXX] FROM [YYY] WHERE [XXX] IS NOT NULL " & _
    "ORDER BY [XXX]"


  Set rs = New ADODB.Recordset
  Set rs.ActiveConnection = cn
  rs.Open sql2
 
  Set Combobox278.Recordsource = rs

HTH,
Daniel
Avatar of egovernment
egovernment
Flag of Afghanistan image

ASKER

Set Combobox278.Recordsource = rs

This is give me an error because the recordsource is not an property to combobox
Avatar of egovernment
egovernment
Flag of Afghanistan image

ASKER

This is urgent
Avatar of danishani
danishani
Flag of United States of America image

Oops that must be:

Set Combobox278.Rowsource = rs

HTH,
Daniel

ASKER CERTIFIED SOLUTION
Avatar of Pratima
Pratima
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of kumaresan2011
kumaresan2011
Flag of India image

hi

try this code

Code Snippet
    Private Sub ReFillCombo()
        Me.ComboBox1.Items.Clear() ' Clear the items of the combobox
        Dim constr As String = "Your Connection String"
        Dim conn As New SqlConnection(constr)
        Dim da As New SqlDataAdapter("Your select command", conn)
        Dim dt As New DataTable
        da.Fill(dt)
        Me.ComboBox1.DisplayMember = "The field which  you want to display"
        Me.ComboBox1.ValueMember = "The value member (mostly used for ID fields)"
        Me.ComboBox1.DataSource = dt
    End Sub

by
kumaresan
Avatar of kumaresan2011
kumaresan2011
Flag of India image

hi

try this codes also

Dim MyConnObj As New ADODB.Connection 'ADODB Connection Object
Dim myRecSet As New ADODB.Recordset 'Recordset Object
Dim sqlStr As String ' String variable to store sql command
MyConnObj.Open _
"Provider = sqloledb;" & _
"Data Source="";" & _
"Initial Catalog=tebrauteguh;" & _
"User ID=sa;" & _
"Password=sa;"
sqlStr = "select * from tblMLeave "
myRecSet.Open sqlStr, MyConnObj, adOpenKeyset
MsgBox "Total Number of records = " & myRecSet.RecordCount
'Dim i As Integer 'variable to keep count
'i = 1
'Print "#"; Tab; "LCode"; Tab; "LName";
'Print ""
With myRecSet
While Not myRecSet.EOF ' Loop until endd fo file is reached
ReaApp.Items.Add (tblMLeave.Item("LName"))
.MoveNext
Wend
End With
MyConnObj.Close

by
kumaresan
Avatar of egovernment
egovernment
Flag of Afghanistan image

ASKER

Still my question is not answer that why the my above code only working with access not sql server ?
Avatar of nike_golf
nike_golf
Flag of Afghanistan image

Which piece of code are you trying to get working?

Is your code getting you a recordset when querying SQL?

    vr_RdSet.Open "SELECT [XXX] FROM [YYY] WHERE [XXX] IS NOT NULL " & _
    "ORDER BY [XXX]", vr_Conn, adOpenStatic, adLockReadOnly

Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo