Link to home
Start Free TrialLog in
Avatar of gcw
gcw

asked on

Simple SQL Question

Does someone have some code that would allow me to access a table in SQL and pass the information found into a textbox??? I need instructions on how to connect and perform the query.. I am very new to SQL.. I am however a fast learner. Any help would be appreciated
Avatar of fguerreiro_inix
fguerreiro_inix

' Create Microsoft Jet Workspace object.
Set wrkJet = CreateWorkspace("", "admin", "", dbUseJet)

' Open Database object from saved Microsoft Jet database
' for exclusive use.

Set dbsNorthwind = wrkJet.OpenDatabase("Northwind.mdb", _
True)

' Create a recordset to get all Employees
Set rstTemp = dbsNorthwind.OpenRecordset( "SELECT *      FROM Employees", dbOpenDynaset, dbReadOnly)

'Set the value of the textbox to Employee name
Textbox1.text = rstTemp("field_name")            

If you need some more explanation, ask for it before grading the question.
Avatar of gcw

ASKER

I need to access a SQL Table not an access Database
Are you using RDO or DAO?
Avatar of gcw

ASKER

DAO
ASKER CERTIFIED SOLUTION
Avatar of TheAnswerMan
TheAnswerMan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gcw

ASKER

how do I get the information from the database and place into a textbox then? I will give more points...
Dim MyRec as Recordset

Set MyRec = conPubs.OpenRecordset( "SELECT * FROM MyTable", dbOpenSnapshot)
if not Myrec.eof then
  Text1 = MyRec!MyField & vbnullstring
end if
for instance.. flop a listbox on a form.

in the form_click event.. do this..
it will populate the listbox with the authors'last name from the authors table in your SQL Server Database..<assuming you didnt take it out>

Sub Form_click()
Dim wrkODBC As Workspace
Dim conPubs As Connection
Dim MyRec As Recordset
'assumes you have a Pubs DSN created for yourself
Set wrkODBC = CreateWorkspace("MySQLWorkspace", _
        "sa", "", dbUseODBC)
Set conPubs = wrkODBC.OpenConnection("", dbDriverPrompt, , _
      "ODBC;DATABASE=Pubs;UID=sa;PWD=;DSN=Pubs;")
Set MyRec = conPubs.OpenRecordset("SELECT au_lname FROM Authors", dbOpenSnapshot)

Do While Not MyRec.EOF
 List1.AddItem MyRec!au_lname & vbNullString
 MyRec.MoveNext
Loop
End Sub
Avatar of gcw

ASKER

thanks! this helps alot :)