473,385 Members | 1,798 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,385 software developers and data experts.

Inserting New Field to SQLServer Back-end via MS Access Front-end

5
Hi,

I have recently converted an Access front/back-end app into a SQL Server (2000) back-end database from a front-end MS Access (2003). The tables are linked via ODBC.

I want to add a new field to one of the tables in the back-end, via the front-end Access by VBA coding.

I have done it in the past by creating a tabledef and appending a field to that, but now when I try that to the SQL server, it doesn't work. Basically it goes to setting the database with the workarea and it breaks there. "set dbData = wrkArea.OpenDatabase.... (below)

Is there a specific way to append to SQL Server? or see something wrong in my connection code? Thanks in advance.

Expand|Select|Wrap|Line Numbers
  1. Dim wrkArea As Workspace
  2. Dim dbData As Database
  3. Dim tbl As TableDef
  4. Dim flds As Fields
  5. Dim fld As Field
  6. Dim strDest As String
  7.  
  8. strDest = GetPath("tblStudents")
  9.  
  10. Set wrkArea = DBEngine.CreateWorkspace("", "Admin", "sa", dbUseODBC)
  11. Set dbData = wrkArea.OpenDatabase(strDest, True)
  12.  
  13. Set tbl = dbData.TableDefs("tblStudents")
  14.  
  15. Set fld = tbl.CreateField("Termination", dbDate)
  16. tbl.Fields.Append fld
  17.  
  18. dbData.Close
  19. wrkArea.Close
Where strDest = ODBC;DRIVER=SQL Server;SERVER=(local);....
Aug 7 '07 #1
2 1847
ck9663
2,878 Expert 2GB
Hi,

I have recently converted an Access front/back-end app into a SQL Server (2000) back-end database from a front-end MS Access (2003). The tables are linked via ODBC.

I want to add a new field to one of the tables in the back-end, via the front-end Access by VBA coding.

I have done it in the past by creating a tabledef and appending a field to that, but now when I try that to the SQL server, it doesn't work. Basically it goes to setting the database with the workarea and it breaks there. "set dbData = wrkArea.OpenDatabase.... (below)

Is there a specific way to append to SQL Server? or see something wrong in my connection code? Thanks in advance.

Expand|Select|Wrap|Line Numbers
  1. Dim wrkArea As Workspace
  2. Dim dbData As Database
  3. Dim tbl As TableDef
  4. Dim flds As Fields
  5. Dim fld As Field
  6. Dim strDest As String
  7.  
  8. strDest = GetPath("tblStudents")
  9.  
  10. Set wrkArea = DBEngine.CreateWorkspace("", "Admin", "sa", dbUseODBC)
  11. Set dbData = wrkArea.OpenDatabase(strDest, True)
  12.  
  13. Set tbl = dbData.TableDefs("tblStudents")
  14.  
  15. Set fld = tbl.CreateField("Termination", dbDate)
  16. tbl.Fields.Append fld
  17.  
  18. dbData.Close
  19. wrkArea.Close
Where strDest = ODBC;DRIVER=SQL Server;SERVER=(local);....

am not 100% familiar with Access. there are a number of ways you can do this. one is to create a stored proc that do the insert. call that proc from your access using a recordset. your proc may return a value 1 or 0 for a successful or failed insert....

you may also build the transact sql (alter table...) as a string variable and execute it in a connection object...
Aug 7 '07 #2
satchi
5
Thanks for the reply ck9663,

I tried creating a connection and running a SQL statement through and it worked great! Thanks!

Expand|Select|Wrap|Line Numbers
  1. Dim wrkArea As Workspace
  2. Dim conn As Connection
  3. Dim dbData As Database
  4. Dim tbl As TableDef
  5. Dim strDest As String
  6. Dim strSQL As String
  7.  
  8. strDest = "ODBC;DRIVER=SQL Server;DSN=FallStudents;SERVER=(local);Database=Students;Trusted_Connection=Yes"
  9. strSQL = "ALTER TABLE tblStudentInfo ADD Termination varchar(30);"
  10.  
  11. Set wrkArea = CreateWorkspace("", "", "", dbUseODBC)
  12. Set dbData = CurrentDb
  13. Set tbl = dbData.TableDefs("tblStudentInfo")
  14. Set conn = wrkArea.OpenConnection("NewConn", , False, strDest)
  15.  
  16. conn.Execute strSQL
  17.  
  18. tbl.RefreshLink
  19.  
  20. dbData.Close
  21. wrkArea.Close
Aug 8 '07 #3

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

Similar topics

2
by: Gary Miller | last post by:
I have a TextBox control with a databings to a dataset.column name. The MaxLength property is 2000 bytes. When I enter data e.g. 1957 bytes and do a Update the screen remains complete, but if I...
0
by: Kristofer Liljeblad | last post by:
Hi, I've successfully setup my machine to use: sessionState mode="SQLServer". All is up and running so no problems so far. Because I curious person, I started to play around with the two new...
4
by: Roger Aikin | last post by:
I've converted a VB6 project to VB.Net 2005. (Actually using vstudio pro 2005). I'm on a machine that also has VB6 and Vstudio.net 2003 on it. The project uses Word, Excel, MapPoint and SqlClient...
7
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function...
9
by: MrHelpMe | last post by:
Hello again experts, I have successfully pulled data from an LDAP server and now what I want to do is drop the data into a database table. The following is my code that will insert the data but...
1
by: kunalm | last post by:
Hello I am trying to insert and then read some text from a textarea control, into a mysql TEXT field, with php. For that I am using simple php code for insertion of records. But the...
1
by: sivakumar83 | last post by:
hi, can anybody guide me how to write code to read data from the css file and insert that in sqlserver database
6
by: Bunty | last post by:
I want to insert values in the database.If i insert values one by one then it works till 4 or 5 fields then after it gives error.In my database there are more than 20 field.Pls help me.
7
by: bobdurie | last post by:
I'm trying to run an import of some data that has unix style carriage returns throughout (ie, 0x0A, or \n). When i use osql to import the data, it shows up in the SQL Server 2005 database as 0x0D...
2
by: masajid686 | last post by:
hi frnds I written the code for inserting records in sqlserver 2005 using dataset and command builder but i dont know hot write sp for that one. can anybody tellme Thanks
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.