Getting data from SQL Server into an MS Access database - sql-server

I am creating an MS Access program using SQL Server.
I added some data in to a table:
I need to select 'Method ID', 'Location', 'Spec1', 'Spec2', 'Spec3, 'Spec4' data to the Access program.
But when I click the combo box, it asks me to 'Enter Parameter Value' for location, Spec1, Spec2, Spec3, Spec4 even though there is the data that I entered in SQL Server:
Do you know how to handle this error?
I have no idea why it does not work.
Any comments would be highly appreciated!

Follow These Steps: (Video also available)
1. Open control Panel
2. open “ODBC” from “Administrative tools”
3. Create a Data Source for the SQL database from which you need to import
4. open MS Access, Select “External Data” Tab
5. Select “ODBC Database”
6. In “Machine Data source” tab, select the Data Source you have created.
7. Then you will find an Import Object window, Select the tables that you need to Import
8. Then Press “OK”
If you prefer to do using ADO : http://www.vb-helper.com/howto_ado_import_sql_server.html
Else use this code:
SELECT * FROM [ODBC;Driver=SQL Server Native Client 10.0;SERVER=Your_Server_Name;DATABASE=DB_Name;UID=User_Login;PWD=Your_Password]
Import or link to SQL Server data
Applies To: Access 2007
If your department or workgroup uses Microsoft SQL Server to store data, you might encounter scenarios where you need to work with some of the SQL Server data in Microsoft Office Access 2007.
If you are not familiar with SQL Server and want to learn more, visit the Microsoft SQL Server home page. Follow the link in the See Also section of this article.
You can bring data from SQL Server objects (tables or views) into Office Access 2007 in either of two ways — by importing, or by linking. If you import the data, you copy the data into your Access database. Any subsequent changes you make to the data in Access are not reflected in the SQL Server database. Likewise, any subsequent changes made in the SQL Server table or view are not reflected in the Access table. By contrast, if you link to SQL Server data, the data remains on the SQL Server computer, and Access maintains a connection to that data. Any subsequent changes that you make to the data in Access are reflected in the SQL Server data, and any changes that you make in the SQL Server database are reflected in the linked table in Access.
This article describes the steps both for importing and for linking SQL Server data into Access 2007.
What do you want to do?
Import SQL Server data
Link to SQL Server data
Learn how Access interprets SQL Server data types
Import SQL Server data
Importing SQL Server data creates a copy of the data in an Access database. During the import operation, you specify the tables or views that you want to copy.
The import operation creates a table in Access and then copies the data from the SQL Server database into the Access table. Find links to more information about tables and how a database is structured in the See Also section.
At the end of the import operation, you can choose to save the details of the import operation as a specification. An import specification helps you to repeat the import operation in the future without having to step through the Import Wizard each time.
Common scenarios for importing a SQL Server table into Access
Typically, you import SQL Server data into an Access database for these reasons:
• You want to permanently move SQL Server data to an Access database because you no longer need the data in your SQL Server database. You can import the data into Access and then delete it from the SQL Server database.
• Your department or workgroup uses Access, but you are occasionally pointed to a SQL Server database for additional data that must be merged into one of your Access databases.
The following steps explain how to import SQL Server data into an Access database.
Prepare for the import operation
1. Locate the SQL Server database that contains the data that you want to copy. Contact the administrator of the database for connection information.
2. Identify the tables or views that you want to copy to the Access database. You can import multiple objects in a single import operation.
3. Review the source data and keep the following considerations in mind:
o Access does not support more than 255 fields in a table, so Access imports only the first 255 columns.
o The maximum size of an Access database is 2 gigabytes, minus the space needed for system objects. If the SQL Server database contains many large tables, you might not be able to import them all into a single .accdb file. In this case, you might want to consider linking the data to your Access database instead.
o Access does not automatically create relationships between related tables at the end of an import operation. You must manually create the relationships between the various new and existing tables by using the options on the Relationships tab. To display the Relationships tab:
On the Database Tools tab, in the Show/Hide group, click Relationships.
4. Identify the Access database into which you want to import the SQL Server data.
Ensure that you have the necessary permissions to add data to the Access database. If you don't want to store the data in any of your existing databases, create a blank database by clicking the Microsoft Office Button , and then clicking New.
5. Review the tables, if any exist, in the Access database.
The import operation creates a table with the same name as the SQL Server object. If that name is already in use, Access appends "1" to the new table name — for example, Contacts1. (If Contacts1 is also already in use, Access will create Contacts2, and so on.)
Note: Access never overwrites a table in the database as part of an import operation, and you cannot append SQL Server data to an existing table.
Import the data
1. Open the destination database.
On the External Data tab, in the Import group, click More.
2. Click ODBC Database .
3. Click Import the source data into a new table in the current database, and then click OK.
4. In the Select Data Source dialog box, if the .dsn file that you want to use already exists, click the file in the list.
I need to create a new .dsn file
Note: The steps in this procedure might vary slightly for you, depending on the software that is installed on your computer.
a. Click New to create a new data source name (DSN).
The Create New Data Source Wizard starts.
b. In the wizard, select SQL Server in the list of drivers, and then click Next.
c. Type a name for the .dsn file, or click Browse to save the file to a different location.
Note: You must have write permissions to the folder to save the .dsn file.
d. Click Next, review the summary information, and then click Finish to complete the wizard.
The Create a New Data Source to SQL Server Wizard starts.
e. In the wizard, type a description of the data source in the Description box. This step is optional.
f. Under Which SQL Server do you want to connect to, in the Server box, type or select the name of the SQL Server to which you want to connect, and then click Next to continue.
g. On this page of the wizard, you might need to get information from the SQL Server database administrator, such as determining whether to use Microsoft Windows NT authentication or SQL Server authentication. Click Next to continue.
h. On the next page of the wizard, you might need to get more information from the SQL Server database administrator before proceeding. If you want to connect to a specific database, ensure that the Change the default database to check box is selected. Then select the database that you want to work with, and then click Next.
i. Click Finish. Review the summary information, and then click Test Data Source.
j. Review the test results, and then click OK to close the SQL Server ODBC Data Source Test dialog box.
If the test was successful, click OK again to complete the wizard, or click Cancel to return to the wizard and make changes to your settings.
5. Click OK to close the Select Data Source dialog box.
Access displays the Import Objects dialog box.
6. Under Tables, click each table or view that you want to import, and then click OK.
7. If the Select Unique Record Identifier dialog box appears, Access was unable to determine which field or fields uniquely identify each row of a particular object. In this case, select the field or combination of fields that is unique for each row, and then click OK. If you are not sure, check with the SQL Server database administrator.
Access imports the data. If you plan to repeat the import operation later, you can save the import steps as an import specification and easily rerun the same import steps later. Go to the next section of this article to complete that task. If you do not want to save the details of the import specification, click Close under Save Import Steps in the Get External Data - ODBC Database dialog box. Access completes the import operation and displays the new table or tables in the Navigation Pane.
Save the import steps as a specification
1. Under Save Import Steps in the Get External Data - ODBC Database dialog box, select the Save import steps check box.
A set of additional controls appears.
2. In the Save as box, type a name for the import specification.
3. Type a description in the Description box. This step is optional.
4. If you want to perform the operation at fixed intervals (such as weekly or monthly), select the Create Outlook Task check box. This creates a task in Microsoft Office Outlook 2007 that lets you run the specification.
5. Click Save Import.
Configure the Outlook task
If you selected the Create Outlook Task check box in the previous procedure, Access starts Office Outlook 2007 and displays a new task. Follow these steps to configure the task.
Note: If Outlook is not installed, Access displays an error message. If Outlook is not configured properly, the Outlook Configuration Wizard starts. Follow the instructions in the wizard to configure Outlook.
1. In the Outlook task window, review and modify the task settings, such as Due date and Reminder.
To make the task recur, click Recurrence and fill in the appropriate information.
This figure shows the task scheduler with some typical settings.
For information about scheduling Outlook tasks, see the article Schedule an import or export operation.
2. When you finish with the task settings in Outlook, on the Task tab, in the Actions group, click Save and Close.
Run a saved task
1. In the Outlook Navigation Pane, click Tasks, and then double-click the task that you want to run.
2. On the Task tab, in the Microsoft Office Access group, click Run Import .
3. Switch back to the Access window, and then press F5 to refresh the Navigation Pane.
4. Double-click the imported table to open it in Datasheet view.
5. Ensure that all of the fields and records were imported, and that there are no errors.
6. Right-click the imported table in the Navigation Pane, and then click Design View on the shortcut menu. Review the field data types and other field properties.
Top of Page
Link to SQL Server data
Linking lets you connect to data without importing that information, so that you can view and edit the latest data both in the SQL Server database and in your Access database without creating and maintaining a copy of the data in Access. If you don't want to copy SQL Server data into your Access database, but instead you want to run queries and generate reports that are based on that data, you should link rather than import.
When you link to a table or view in a SQL Server database, Access creates a new table (often referred to as a linked table) that reflects the structure and contents of the source object. You can make changes to data either in SQL Server, or in Datasheet view or Form view from within Access. The changes that you make to data in one place are reflected in the other. However, if you want to make structural changes, such as removing or changing a column, you must do so from within the SQL Server database, or from within an Access project that is connected to that database. You cannot add, delete, or modify the fields in a linked table while working in Access.
If the SQL Server database contains a large amount of data, you should choose linking over importing, because the maximum size of an Access database is 2 gigabytes (minus the space needed for system objects). Importing many large tables or views can cause you to exceed that limit, whereas linking to the data does not add much size to your Access database.
Common scenarios for linking to SQL Server data
Typically, you link to a SQL Server table or view from an Access database for these reasons:
• Your department or workgroup uses Access for reporting and querying and uses SQL Server for data storage. Individual teams can create SQL Server tables and views for centralized storage, but often this data must be brought into desktop programs for aggregation and reporting. Linking is the appropriate choice, because it allows users of both the SQL Server database and the Access database to add and update data, and to always view and work with the latest data.
• You are an Access user who recently started using SQL Server. You migrated several of your databases to SQL Server, and most of the tables in these databases are linked tables. From now on, instead of creating Access tables, you will create tables and views in SQL Server and then link to them from your Access databases.
• You want to continue storing your data in SQL Server, but you also want to work with the most recent data inside Access in order to run queries and print reports that you designed in Access.
Prepare to link to a SQL Server table
1. Locate the SQL Server database that has the data to which you want to link. Contact the database administrator for connection information.
2. Identify the tables and views to which you want to link. You can link to multiple objects in a single linking operation.
3. Review the source data and keep the following considerations in mind:
o Access does not support more than 255 fields in a table, so the linked table will include only the first 255 fields of the object you link to.
o The columns that are read-only in a SQL Server object will continue to be read-only in Access.
o You will not be able to add, delete, or modify columns in the linked table in Access.
4. Identify the Access database in which you want to create the linked tables. Ensure that you have the necessary permissions to add data to the database. If you don't want to store the data in any of your existing databases, create a new, blank database by using the following command:
Click the Microsoft Office Button , and then click New.
5. Review the tables in the Access database. When you link to a SQL Server table or view, Access creates a linked table with the same name as the source object. If that name is already in use, Access will append "1" to the new linked table name — for example, Contacts1. (If Contacts1 is also already in use, Access will create Contacts2, and so on.)
Link to the data
1. Open the destination database.
2. On the External Data tab, in the Import group, click More.
3. Click ODBC Database.
4. Click Link to the data source by creating a linked table, and then click OK.
5. In the Select Data Source dialog box, click the .dsn file that you want to use, or click New to create a new data source name (DSN).
6. In the Select Data Source dialog box, if the .dsn file you want to use already exists, click the file in the list.
I need to create a new .dsn file
Note: The steps in this procedure might vary slightly for you, depending on the software that is installed on your computer.
a. Click New to create a new data source name (DSN).
The Create New Data Source Wizard starts.
b. In the wizard, select SQL Server in the list of drivers, and then click Next.
c. Type a name for the .dsn file, or click Browse to save the file to a different location.
Note: You must have write permissions to the folder to save the .dsn file.
d. Click Next, review the summary information, and then click Finish to complete the Create New Data Source Wizard.
The Create a New Data Source to SQL Server Wizard starts.
e. In the wizard, type a description of the data source in the Description box. This step is optional.
f. Under Which SQL Server do you want to connect to, in the Server box, type or select the name of the SQL Server computer to which you want to connect, and then click Next to continue.
g. On this page of the wizard, you might need to get information from the SQL Server database administrator, such as whether to use Windows NT authentication or SQL Server authentication. Click Next to continue.
h. On the next page of the wizard, you might need to get more information from the SQL Server database administrator. If you want to connect to a specific database, ensure that the Change the default database to check box is selected, select the SQL Server database that you want to work with, and then click Next.
i. Click Finish. Review the summary information, and then click Test Data Source.
j. Review the test results, and then click OK to close the SQL Server ODBC Data Source Test dialog box.
If the test was successful, click OK again to complete the wizard, or click Cancel to return to the wizard and make changes to your settings.
7. Click OK.
Access displays the Link Tables dialog box.
8. Under Tables, click each table or view that you want to link to, and then click OK.
9. If the Select Unique Record Identifier dialog box appears, Access was unable to determine which field or fields uniquely identify each row of the source data. In this case, select the field or combination of fields that is unique for each row, and then click OK. If you are not sure, check with the SQL Server database administrator.
Access completes the linking operation and displays the new linked table or tables in the Navigation Pane.
Important: Each time you open either a linked table or the source object, you see the latest data displayed in it. However, structural changes made to a SQL Server object are not automatically reflected in a linked table.
To update a linked table by applying the latest SQL Server object structure:
1. Right-click the table in the Navigation Pane, and then click Linked Table Manager on the shortcut menu.
2. Select the check box next to each linked table that you want to update, or click Select All to select all of the linked tables.
3. Click OK.
If the update is successful, Access displays a message to that effect. Otherwise, Access displays an error message.
4. Click Close to close the Linked Table Manager.
Top of Page
Learn how Access interprets SQL Server data types
Because Access data types differ from SQL Server data types, Access must determine the most appropriate Access data type to use for each column of each SQL Server table or view that you import or link to. For example, a SQL Server column of the data type bit is imported or linked into Access with the data type Yes/No. As another example, a SQL Server column of the data type nvarchar(255) (or smaller) is imported or linked into Access with the data type Text, but a column of the data type nvarchar(256) (or larger) is brought in as an Access Memo field. After completing an import or linking operation, you should open the table in Design view and confirm which data types Access assigned to its fields. You can change the data types of fields in imported tables; however, you cannot change the data types of fields in linked tables, except in the SQL Server database or in an Access project that is connected to that database.
The following table lists the main SQL Server data types. The second and third columns show how Access interprets each type.
SQL Server data type Access data type Access field size
bigint Text 255
binary( field size ) Binary Same as SQL Server field size
bit Yes/No
char( field size ), where field size is less than or equal to 255 Text Same as SQL Server field size
char( field size ), where field size is greater than 255 Memo
datetime Date/Time
decimal( precision , scale ) Number Decimal (Access Precision and Scale properties match SQL Server precision and scale.)
float Number Double
image OLE Object
int Number Long Integer
money Currency
nchar ( field size ), where field size is less than or equal to 255 Text Same as SQL Server field size
nchar ( field size ), where field size is greater than 255 Memo
ntext Memo
numeric( precision , scale ) Number Decimal (Access Precision and Scale properties match SQL Server precision and scale.)
nvarchar ( field size ), where field size is less than or equal to 255 Text Same as SQL Server field size
nvarchar ( field size ), where field size is greater than 255 Memo
nvarchar (MAX) Memo
real Number Single
smalldatetime Date/Time
smallint Number Integer
smallmoney Currency
sql_variant Text 255
text Memo
timestamp Binary 8
tinyint Number Byte
uniqueidentifier Number Replication ID
varbinary Binary Same as SQL Server field size
varbinary (MAX) OLE Object
varchar ( field size ), where field size is less than or equal to 255 Text Same as SQL Server field size
varchar ( field size), where field size is greater than 255 Memo
varchar (MAX) Memo
xml Memo
https://support.office.com/en-us/article/Import-or-link-to-SQL-Server-data-a5a3b4eb-57b9-45a0-b732-77bc6089b84e?CorrelationId=7b7e6535-5ac3-4fd1-9766-d7ebfa151046&ui=en-US&rs=en-US&ad=US&fromAR=1&ocmsassetID=HA010200494

Related

Re-linking Tables in Access Database

First of all - my VB skills are almost non-existent so any suggestion/ideas should be a little more elaborate if possible. Here is the problem I am facing -
We have linked database tables and queries in Ms Access 2010. Now a copy of the database has been created on a new SQL server. So I want to re-link the tables so that access points to the tables on the new SQL server. Right now, all the tables are corresponding to the database on the old server. Here is what I have tried -
Open XYD_Outreach_Dev.accdb on the network (this is the dev version of access that we use)
Form a new ODBC connection which creates a new_sql_server.dsn file corresponding to the new SQL server (2012)
Go to External Data -> ODBC Database -> Link to the data source by creating......
Link to a table say - tblAbriContacts
This creates a new linked table dbotblAbriContacts
I rename it to tblAbriContacts at which point Access asks me -> table already exists. Do you want to replace it? I click yes and the table gets replaced
I hit the save button, close the application and restart it. The tblAbriContacts is pointing to the old database again
What could be the issue? Please note that I have to do this for all the database, lookup and junction tables.
Do I have to change some connection string which the access points to as soon as it is launched?
If I try the Linked table manager, nothing happens, it says "All selected tables were successfully refreshed". But nothing happens. I guess that's because the tables haven't been moved from the old database. A new copy of everything has been created on the new server.
Please help me out with this. My work is stuck because of this reason.
Can you please check if there is a module or other vba file created and file's on_load event has some code which to assign the connection string for all objects (tables and SPs) and refreshes it every time you restart the application.
If you see the connection string there, you might need to replace it with the new one.

Insert table data from table to another table

I'm trying to insert from one database to another using SQL Server 2008.
I'm trying to insert from 'EG-COMPUTER1\FTLIVE' to 'EG-COMPUTER2\FTSTANDBY'.
Basically from a database on one computer to the same database on a different computer which is on the same network.
How would I able to do this?
Note: I can connect to both databases on the same computer so there must be an easy way to insert from one database to another?
EDIT::
Just to add that one computer is using SQL Server 2008-R2 and the other is using SQL Server 2008
There are several ways you can do this.
1. RightClick On DataBase -> Tasks -> Import -> Import Export wizard ->Choose Source database -> choose Destination database -> choose Copy data from One or more table -> choose required tables -> Import.
2.RighClick On Database -> Task -> Generate Script -> Choose tables -> Advanced -> Change "Types of Data to Script " to "Schema and Data or data or schema(as required)" -> Change "script Drop and Create" to Drop and Create(optional) - > create.
Copy the script file/files and run in the other computer.
3.Right Click the database -> task -> BackUp -> create backup -> take the back up to other computer(.bak file can be found inside the Backup directory inside installation directory) -> right click the database -> task -> restore -> choose the bakup file -> done.
4. Can use sql azure migration tool http://sqlazuremw.codeplex.com/ (similar to step 1)
If you would like to just use the database level transfer of data then you should configure those two instances as linked servers (msdn doc).
Otherwise, if you are familiar with Java (or any high level language that provides APIs to interact with databases), you can write a simple program that retrieves the data from the table in one server and inserts into the table in the other server (quicker way without reaching out to DBAs to configure linked servers).
I've found what I need to do, this is:
INSERT INTO [DatabaseNameA].[dbo].[Table]
([Column1],[Column2] etc...)
SELECT
[Column1],[Column2] etc...
SET IDENTITY_INSERT [DatabaseNameB].[dbo].[Table] On;
GO
This will select the database I want to select from, turn the identity_insert to on so the data can be inserted and transfer the data from one database to another.
Nislva ,
kindly verify both tables are acessable or not with your login. if both tables are acessable then try to copy of one database table to another database table other wise create one view from one database table and transfer that view to another database table.
there after you can do your required task. this is best practice method and in future also you may not get performance problems.
if you are not intrested to do this task then simply follow the below query
insert into ur-tablename(column-names)
select sourcedbname.schemaname.column-name1 , sourcedbname2.schemaname.column-name ...
from sourcedatabsename.schemaname.tablename
You can either use OpenRowSet if this is not a recurring activity otherwise if you need to perform actions frequently then you should create linked server.

Importing tab-delimited text file - can't change the column mappings SQL Server 2008

I have a very large tab-delimited text file I'm trying to import into SQL Server 2008. Some of the field names are greater than 50 characters and when I try to change the column mappings using Management Studio, I'm unable to change the data type (default is varchar) or the size (default is 50). The Edit SQL button is also grayed out. What gives with this?
I am importing the data through the import wizard in SSMS (right click on database name, tasks, import)
Sometimes, if you run the wizard and you get an error, it creates a table. You have to go into SQL, delete the table and then start over. Otherwise, SQL Wizard does not allow you to edit the table once it has been created.

How to change destination table name in SQL Server Import and Export Wizard

I use SQL Server Import and Export Wizard to transfer data from a remote server to my local server.
First step, Choose a Data Source.
Second step, Choose a Destination.
Third step, Specify Table Copy or Query, there are 2 options
I choose Write a query to specify the data to transfer because I need to organize the data on remote server first and then transfer the result.
Then I specify the SQL statement I want to run on remote server.
I found that the destination table name on my local server is forced to be 'Query'.
How can I change this from Query to something I specify?
On the screen after you select the query you get the 'Select Source Tables and Views' screen. On this screen you can select which table the data is written to in the destination column. This drop down will let you select any table in the destination database. So you will need to create your destination table in the destination database before using the wizard.
You can also edit column mappings and what happens to existing data by using the 'Edit Mappings...' button.
You are basically creating an ssis package via a wizard. Create the package manually, create a table destination variable, figure out a way to populate the table destination variable, and choose "Table or view name from variable" (choosing that variable) for the destination.

Best (easiest) way to make a SQL Server dump and import that dump in another SQL Server

I would like to achieve a database export (dump) in SQL Server from one server and import that dump in another SQL Server and not necessarily in the same schema name.
For example if I have a database prepared with all the data set for implement a new DB for a new customer, that db is for example named DB_EMPTY
And then I have to setup the same DB on some external server for a customer for example in the schema DB_MY_CUSTOMER
What is the best/simplest way to export (dump) a DB_EMPTY, and import it in DB_MY_CUSTOMER?
Possibly with SQL Server Management Studio?
An easy way would be to use SQL Server Management Studio, in the Object Explorer right click on the database you want to export, select Tasks -> Back Up, then select a destination and file name in the Destination box at the bottom of the dialog. You can play around with the various settings, but you don't need to.
To restore it on another server is basically the opposite, choose Tasks -> Restore -> Database, in the dialog select From Device, then click the browse ellipsis, from there you get a browse dialog, click Add and then navigate to the back up file you created. You can change the databse name in the To database textbox, and control where the files get stored by going to the Options tab and altering the Restore As column entries in the table labelled as Restore the database files as:.
Just in case someone is ending here and noticing "Tasks -> Back up" option does not exists; on recent versions of SSMS ( v18 for example ), you'll need to use "Generate scripts" option and then on "Advanced" select "Schema and Data" on "Types of data to script".
Credits to: http://statmap.co.uk/?page_id=9207
This is called replication : http://databases.about.com/od/sqlserver/ht/distribution.htm
Try to look to the snapshot replication. It is configurable to determine the destination database.

Resources