Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS access to SQL Server

Posted on 2014-03-31
3
Medium Priority
?
558 Views
Last Modified: 2014-04-02
I need to connect my Access 2010 form with SQL Server 2012 table ”tblMaster”  seating in SQL Server database named “Production_db” .
I would like to use ADO to connect and pull out some records from table” tblMaster”.
For that reason I probably need to initiate reference Microsoft Active X Data Object control  2.8 in MS Access reference settings
I would like to have command button on form which I click and make connection to table in SQL Server database  then select  particular record  from table and then display it on form fields.
 My PC is on company network.
SQL Server database is seating on one company server named Prod_Serv  that is on company network too.
I need code that will be behind this command button.
0
Comment
Question by:Taras
3 Comments
 
LVL 40

Expert Comment

by:PatHartman
ID: 39967886
Is there some particular reason that you don't want to use a linked table and a bound form?
0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 total points
ID: 39968921
If you can't use linked tables, then you must do this:

1. Create an ADO or DAO connection. Since you're dealing with server-based data, ADO would be the better choice, and as you indicate you would need to set a reference to the ADO library:

Dim con As New ADODB.Connection
con.ConnectionString = "Your Connection String"
con.Open

Here's a good resource for connection strings: http://www.connectionstrings.com/sql-server/

2. Create a recordset using that connection:

Dim rst As New ADODB.Recordset
rst.Open "SELECT * FROM SomeTable WHERE IDField=123", con

You should then have the data from "SomeTable" in that recordset, for the defined criteria. You could then use one of several methods to show that data. If it's just a few fields, you could do this:

Me.Textbox1 = rs("Field1")
Me.Textbox2 = rs("Field2")
and so on

If there are a LOT, then you can (a) create textboxs with EXACTLY the same name as the recordset fields and then (b) loop through the Fields collection of the recordset and set the value:

Dim i as Integer
For i = 0 to rs.Fields.Count - 1
  Me.Controls(rs.Fields(i).Name) = rs.fields(i)
Next i
0
 

Author Closing Comment

by:Taras
ID: 39972132
Thanks a lot
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question