473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Updating SQL Server Table Using Excel

9
Hi there,

I am using SQL Server 2000 and have users that would like to insert and / or update an existing table using Excel as Excel is a tool they understand. Does anyone have an idea how to do that? If you think Access would be easier, I'm sure I could get my users to turned on to that instead. Any help is greatly appreciated. (;>)
May 3 '06 #1
4 34683
cweiss
36
You can do this using DAO or ADO, I believe. You might have an easier time of it using Access, but using Excel is also possible.

In access, you can build a link to the remote table by going to Insert->Table-Link Table. When the file browser pops up, at the bottom where you see Files of Type, select ODBC Databases (should be the last option). You'll get a pop-up that allows you to select the data source. It may be listed on the Machine Data Source tab, if not you'll need to build it (Add New).

After you create your link, you can work with this table just like you would any other table in access (create an append query, append new records).

I've only done this with an Oracle database though, so it could differ with SQL Server.

If you want to go the Excel route, you'll need to write code to handle it. You can use either ADO or DAO for that.

There are probably other ways to do this as well, but those are the methods I generally use if I have to go through Excel or Access.
May 3 '06 #2
CaptainD
135 100+
I have a budget Excel sheet that, through a module in VBA using ADO and VBA events access a SQL Database, populates the sheet, saves and deletes records etc.

Here is the connection "Functions" in the Module with a few changes.
Expand|Select|Wrap|Line Numbers
  1. 'You could remove the global Const, I have it here because I use this on test databases 'as well as production and it's easier to just make one entry
  2.  
  3. Global Const UseDatabase = "BudgetRequest2006_07SQL"
  4.  
  5.  
  6.  
  7. Function GetADORS(strSql As String) As ADODB.Recordset
  8.  
  9.  
  10. Dim objConn As ADODB.Connection
  11.  
  12. 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  13.     ' Note, UseDatabase is a Global constant located in ModADO
  14.     ' Change the Name there to set which Database to use
  15.     'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  16.  
  17.  
  18. Set objConn = New ADODB.Connection
  19. objConn.Open "Provider=sqloledb.1;data source=YourServerHere;Initial catalog=" & UseDatabase & ";Integrated Security = SSPI;"
  20.  
  21. Set GetADORS = New ADODB.Recordset
  22. GetADORS.Open strSql, objConn, adOpenStatic, adLockReadOnly
  23.  
  24. End Function
  25. Function ADOExecuteSql(strSql As String)
  26. On Error GoTo Errhandler
  27. Dim objConn As ADODB.Connection
  28. Set objConn = New ADODB.Connection
  29.  
  30.  
  31. 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  32.     ' Note, UseDatabase is a Global constant located in ModADO
  33.     ' Change the Name there to set which Database to use
  34.     'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  35.  
  36.  
  37. objConn.Open "Provider=sqloledb.1;data source=YourServerHere;Initial catalog=" & UseDatabase & ";Integrated Security=SSPI;"
  38. objConn.Execute (strSql)
  39. objConn.Close
  40. Exit Function
  41. Errhandler:
  42.     MsgBox "Error executing the SQL statement, error # " & Err.Number & ", " & Err.Description, vbOKOnly, "Error"
  43.         objConn.Close
  44. End Function
  45.  
  46. Function OpenADORS(strSql As String) As ADODB.Recordset
  47.  
  48. Dim objConn As ADODB.Connection
  49. Set objConn = New ADODB.Connection
  50.  
  51.  
  52. 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  53.     ' Note, UseDatabase is a Global constant located in ModADO
  54.     ' Change the Name there to set which Database to use
  55.     'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  56.  
  57. objConn.Open "Provider=sqloledb.1;data source=YourServerHere;Initial catalog=" & UseDatabase & ";Integrated Security=SSPI;"
  58.  
  59. Set OpenADORS = New ADODB.Recordset
  60. OpenADORS.Open strSql, objConn, adOpenDynamic, adLockOptimistic
  61.  
  62.  
  63.  
  64. End Function
  65. Function GetADORSP(strSql As String) As ADODB.Recordset
  66. On Error GoTo Err_GetADORSP
  67.  
  68. Dim objConn As ADODB.Connection
  69.  
  70.  
  71. Set objConn = New ADODB.Connection
  72. objConn.Open "Provider=sqloledb.1;data source=YourServerHere;Initial catalog=" & UseDatabase & ";Integrated Security=SSPI;"
  73.  
  74. Set GetADORSP = New ADODB.Recordset
  75. GetADORSP.Open strSql, objConn, adOpenStatic, adLockReadOnly
  76.  
  77. Exit Function
  78. Err_GetADORSP:
  79.     MsgBox "Error getting data, error #" & Err.Number & ", " & Err.Description
  80.  
  81. End Function
  82.  
You need to reference ADO 2.0 or higher

In your events you would do something like this
Expand|Select|Wrap|Line Numbers
  1. Dim rs as adodb.recordset
  2. Dim strSql as string
  3.  
  4. strSql = "Select query here"
  5. Set rs = GetADORS(strSql)
  6.  
  7. 'Work with the data
  8.  
  9. Set rs = nothing
  10.  
May 3 '06 #3
MikeG
9
Thanks CaptainD.
This is quite a bit more programming than I had hoped for as I could probably develop a small Java app using JDBC to do this.

I have found that Access can quite easily be linked to SQL Server tables for ease of maintenance so I have gon e that route.

Many thanks for the code.

Rgds


I have a budget Excel sheet that, through a module in VBA using ADO and VBA events access a SQL Database, populates the sheet, saves and deletes records etc.

Here is the connection "Functions" in the Module with a few changes.
Expand|Select|Wrap|Line Numbers
  1. 'You could remove the global Const, I have it here because I use this on test databases 'as well as production and it's easier to just make one entry
  2.  
  3. Global Const UseDatabase = "BudgetRequest2006_07SQL"
  4.  
  5.  
  6.  
  7. Function GetADORS(strSql As String) As ADODB.Recordset
  8.  
  9.  
  10. Dim objConn As ADODB.Connection
  11.  
  12. 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  13.     ' Note, UseDatabase is a Global constant located in ModADO
  14.     ' Change the Name there to set which Database to use
  15.     'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  16.  
  17.  
  18. Set objConn = New ADODB.Connection
  19. objConn.Open "Provider=sqloledb.1;data source=YourServerHere;Initial catalog=" & UseDatabase & ";Integrated Security = SSPI;"
  20.  
  21. Set GetADORS = New ADODB.Recordset
  22. GetADORS.Open strSql, objConn, adOpenStatic, adLockReadOnly
  23.  
  24. End Function
  25. Function ADOExecuteSql(strSql As String)
  26. On Error GoTo Errhandler
  27. Dim objConn As ADODB.Connection
  28. Set objConn = New ADODB.Connection
  29.  
  30.  
  31. 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  32.     ' Note, UseDatabase is a Global constant located in ModADO
  33.     ' Change the Name there to set which Database to use
  34.     'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  35.  
  36.  
  37. objConn.Open "Provider=sqloledb.1;data source=YourServerHere;Initial catalog=" & UseDatabase & ";Integrated Security=SSPI;"
  38. objConn.Execute (strSql)
  39. objConn.Close
  40. Exit Function
  41. Errhandler:
  42.     MsgBox "Error executing the SQL statement, error # " & Err.Number & ", " & Err.Description, vbOKOnly, "Error"
  43.         objConn.Close
  44. End Function
  45.  
  46. Function OpenADORS(strSql As String) As ADODB.Recordset
  47.  
  48. Dim objConn As ADODB.Connection
  49. Set objConn = New ADODB.Connection
  50.  
  51.  
  52. 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  53.     ' Note, UseDatabase is a Global constant located in ModADO
  54.     ' Change the Name there to set which Database to use
  55.     'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  56.  
  57. objConn.Open "Provider=sqloledb.1;data source=YourServerHere;Initial catalog=" & UseDatabase & ";Integrated Security=SSPI;"
  58.  
  59. Set OpenADORS = New ADODB.Recordset
  60. OpenADORS.Open strSql, objConn, adOpenDynamic, adLockOptimistic
  61.  
  62.  
  63.  
  64. End Function
  65. Function GetADORSP(strSql As String) As ADODB.Recordset
  66. On Error GoTo Err_GetADORSP
  67.  
  68. Dim objConn As ADODB.Connection
  69.  
  70.  
  71. Set objConn = New ADODB.Connection
  72. objConn.Open "Provider=sqloledb.1;data source=YourServerHere;Initial catalog=" & UseDatabase & ";Integrated Security=SSPI;"
  73.  
  74. Set GetADORSP = New ADODB.Recordset
  75. GetADORSP.Open strSql, objConn, adOpenStatic, adLockReadOnly
  76.  
  77. Exit Function
  78. Err_GetADORSP:
  79.     MsgBox "Error getting data, error #" & Err.Number & ", " & Err.Description
  80.  
  81. End Function
  82.  
You need to reference ADO 2.0 or higher

In your events you would do something like this
Expand|Select|Wrap|Line Numbers
  1. Dim rs as adodb.recordset
  2. Dim strSql as string
  3.  
  4. strSql = "Select query here"
  5. Set rs = GetADORS(strSql)
  6.  
  7. 'Work with the data
  8.  
  9. Set rs = nothing
  10.  
May 4 '06 #4
MikeG
9
Thanks cweiss. I discovered myself yesterday that approach you outlined below for Access. It is easy to implement and use. All my users have Access installed on their systems so this is the solution I have converged on.

CaptainD has also elaborated on the approach you outline for Excel. His code is part of a response to this thread for other users who are interested in Excel, albeit more complicated.

Rgds



You can do this using DAO or ADO, I believe. You might have an easier time of it using Access, but using Excel is also possible.

In access, you can build a link to the remote table by going to Insert->Table-Link Table. When the file browser pops up, at the bottom where you see Files of Type, select ODBC Databases (should be the last option). You'll get a pop-up that allows you to select the data source. It may be listed on the Machine Data Source tab, if not you'll need to build it (Add New).

After you create your link, you can work with this table just like you would any other table in access (create an append query, append new records).

I've only done this with an Oracle database though, so it could differ with SQL Server.

If you want to go the Excel route, you'll need to write code to handle it. You can use either ADO or DAO for that.

There are probably other ways to do this as well, but those are the methods I generally use if I have to go through Excel or Access.
May 4 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Robert Fitzpatrick | last post by:
I am running PostgreSQL 7.4.5 and have a trigger on a table called tblriskassessors which inserts, updates or delete a corresponding record in tblinspectors by lookup of a contact id and license...
0
by: beary | last post by:
I am using php5 with mysql and also using excel 2003 running on winxp. Anyway, I am currently opening my excel.xls file, then saving as csv file, then closing, then copying it over to the web...
4
by: somanyusernamesaretakenal | last post by:
What I am trying to achieve: Basically I have generated a report in access. This report needs to be updated using excel. (Updating the new data, not changing existing data) What I did was I...
0
by: OldStd | last post by:
Updating data using 2 data sets I am having some problems in updating the database using two datasets as suggested by someone. 1. Data is displayed in a data grid from a dataset generated using...
0
by: =?Utf-8?B?RGFuaWVs?= | last post by:
Hello i've got a little bit of problem. i wrote a small script that will update a record using vb.net for the web but i set my table so that whenever a record is update to autmatically to get the...
3
by: Ciara9 | last post by:
I am having problems trying to update a field in a database using a field in a form. I currently have two fields, Today and Tomorrow in a table named Date. The Today field automatically defaults to...
0
by: comp21 | last post by:
Hi, Now, I have retrieved or imported data from excel spreadsheet to vb6 application. Now I want to compare this part of data with the one already existing in mssql2000 server(table name being...
2
by: alnoir | last post by:
I'm trying to update some records using the UPDATE and SELECT query. I have two databases. The first database (db1) is a subset of the second database (db2). However, the first database is...
0
LoanB
by: LoanB | last post by:
Hey guys need some info here. I am adding, updating and deleting data on a datagrid. I pass the datagrid's datasource (datatable dt) to my data layer. Assuming I have already created and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.