Access ODBC DB using SQL and a Data Control

By | 2002-06-01

This code will allow you to populate DBGrids and other bound data controls using a Data Control, SQL, and an ODBC DataBase.

Original Author: Scott Lewis

Assumptions

Create a new form with a DBGrid and a Data Control. Set the datasource property on the grid to use the data control. Set the DB name property on the data control to whatever DB you set up in the ODBC applet on control panel. Set the DB type property to Use ODBC. Set Visible to false.

Code

'This code assumes a DB with a table named "Appointments" and fields named '"AppName", "AppTime", "Appointment", and "Notes".
'put this into the Form_Load() area of the form the grid and data
'control are on.
  Data1.RecordSource = ""
  Data1.RecordSource = ReturnFieldsSQL
  Data1.Refresh
  DBGrid1.Refresh
'put this function in a module
Public Function ReturnFieldsSQL()
   Dim SQLS As String
   SQLS = "SELECT AppDate,"
   SQLS = SQLS + " " & "Apptime,"
   SQLS = SQLS + " " & "Appointment,"
   SQLS = SQLS + " " & "Notes"
   SQLS = SQLS + " " & "From [Appointments]"
   ReturnFieldsSQL = SQLS
End Function
'And thats all there is to it.
'This is a very simple function to use.
'You can alter the number of items to return.
'I'm still working on the syntax for the "Where" clause to go with this 'function.
'Once the form loads, if you do it right,
'the grid will be filled with the tables specified here.

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.