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

Connecting to a SQL database or Linking

347 100+
have an application that I'm trying to finish in VBA, and as I've never written in it, it's a bit challenging to me. I've written some in VB but the syntax isn't the same between the two for some things. Essentially, here's the logic (or lack of) that I'm trying to accomplish with this task. This is a payroll program that was written by someone who is no longer with our organization. I am trying to finalize this, and realized that when reviewing it for finalization, that it was missing some components. Currently there is no way to create an audit trail for this program (which is why the timestamp is now coming into play.) Also, there is a value (the option group) that wasn't in the original version of this app. As far as the other two buttons on the first form, those perform calculations that end with a result when a user presses button #3 on the main form. What I'd like that 3rd button to also do is to open the second form, post the data from the results on the first form, show the option group with a submit button, when the user presses the submit button on form2, it appends a timestamp and the options group selection to the same table that the results of form1 were posted to. I'll go back through my code today and clean it up. I hope that that makes more sense now as to what I'm trying to achieve.

Step by step ... this is how the program should work:
1. User presses buttons 1, 2 and 3 on main form and gets a "sum" for the payroll for a specific time frame.
2. When the user presses the last button on main form, the form closes, the sum of the data is posted to a SQL database and a second form is opened.
3. The second form is a option group with 3 options, 1, 2, or 3 and a submit button.
4. The user chooses an option and presses the submit button on the second form, which then posts the option group result AND the timestamp to the SQL table. (the same SQL table where the information from the main form is posted)
5. Form 2 closes.

here is my code thus far: (main form)
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command2_Click()
  2.  
  3. Dim rs As DAO.Recordset
  4. Dim sqlStmt As String
  5.  
  6.  
  7.  On Error GoTo Command2_Click_Err    'Error reporting on query code
  8.  
  9. DoCmd.OpenQuery "DeleteExecupayTable", acViewNormal, acEdit
  10. DoCmd.OpenQuery "5_ExcepToExcupay", acViewNormal, acEdit
  11. DoCmd.OpenQuery "7_SumToExecupay", acViewNormal, acEdit
  12. DoCmd.OpenTable "6_1_Execupay", acViewNormal, acReadOnly
  13. DoCmd.OpenForm "Form2", acNormal
  14.  
  15. Command2_Click_Exit:
  16. Exit Sub
  17.  
  18. Command2_Click_Err:
  19.  MsgBox "Open Query code failed. " & Error$
  20. Resume Command2_Click_Exit
  21.  
  22.  DateStampError:
  23. MsgBox "DateStamp code failed. " & Error$
  24. Resume Command2_Click_Exit
  25.  
  26.  
  27.  End Sub
  28.  
and my second form:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  Dim batchid As String
  3.  
  4.  
  5.  Dim sConn As String
  6.  sConn = "Provider='SQLOLEDB';Data Source='xxxxx';" & _
  7.          "Initial Catalog='xxxxx';"
  8.  
  9.  Set sConn = New ADODB.Connection
  10.  .Open
  11.  
  12.  On Error GoTo DateStampError 'Error reporting on DateStamp code
  13.  sqlStmt = "Update timestamp FROM [Payroll]"
  14.  Set rs = CurrentDb().OpenRecordset(sqlStmt)
  15.  
  16.  With rs
  17.  If .RecordCount = 0 Then
  18.     .AddNew 'For first time use before a record added
  19.     .Fields("fldDate") = Date
  20.     .Update
  21. Else
  22.     .MoveFirst
  23.     .Edit
  24.     .Fields("fldDate") = Date
  25.     .Update
  26. End If
  27. End With
  28. rs.Close
  29. Set rs = Nothing
  30.  
  31.  SelectCase Me.Frame7.Value
  32.  Case 1
  33.  batchid = "='1'"
  34.  Case 2
  35.  batchid = "='2'"
  36.  Case 3
  37.  batchid = "='3'"
  38.  End Select
  39.  
  40.  DateStampError:
  41.  MsgBox "DateStamp code failed. " & Error$
  42.  Resume Command2_Click_Exit
  43.  End Sub
  44.  
and I've even tried to link them together with this code:

DoCmd.TransferDatabase acLink, "ODBC Database", _
"ODBC;Driver={SQL Server};Server=xxxxx;Database=xxxxx;" _
Uid=xxxxx;Pwd=xxxxx",acTable,"Payroll","6_1_Execup ay"

but I get an error of "end of expression expected" but I'm not sure what I'm missing.

Can someone please assist?

Thank you

Doug
Sep 6 '11 #1
9 1964
patjones
931 Expert 512MB
There's definitely a lot to absorb in your post, and as a result it's hard for me to see relevance of all of it to your ultimate question. But I DO appreciate that you've given such a comprehensive overview.

Let me start by first asking what you mean by "linking them together"?

Pat
Sep 6 '11 #2
dougancil
347 100+
Well I know that you can create linked tables in access, and what I'd like to do is link the current access database (which will be a localized database) to a SQL database which will be networked.
Sep 6 '11 #3
NeoPa
32,556 Expert Mod 16PB
You're still not making it clear which of the two you want as the Front End, and which the Back Doug.
Sep 7 '11 #4
patjones
931 Expert 512MB
Good morning,

I recently started connecting Access front ends to SQL Server. I'm not aware of the DoCmd.TransferDatabase method, though that's not to say that it isn't valid.

What I have done in two recent projects was to use ODBC. You can get started with this in Access 2007 by going External Data > More> ODBC> Link to the data source by creating a linked table and then clicking OK.

This method requires something called a DSN (Domain Server Name) file, which tells Access how to connect to your SQL Server database. Setting up the DSN is something I had to ask my IT division to do. Once I had the DSN though, it was a smooth process. Access connects to SQL, and you get a list of tables that you can link to. The end result is a set of linked tables that you can then reference in VBA just like any other table (for the most part, but there are some nuances of course).

It is possible to connect to SQL Server by using connection strings embedded within VBA, along lines similar to what you're trying to do. You would then manage data using pass-through queries and other constructs that I'm probably not aware of. The use of connection strings can get tricky and it often takes a bit of tweaking to get them to work properly. Also, maintaining the SQL connection from VBA will increase the amount of code that you have to manage. I've used both methods, but I do like ODBC.

Does this provide some insight for you?

Pat
Sep 7 '11 #5
dougancil
347 100+
Neo,

The Main form is the front end and the second form is the backend. Pat, I'll try to set up the DSN for that server today, what about the rest of the code, once the tables are linked, am I able to save the access table to the SQL table, add the values from my frame and then timestamp the data?

Thank you

Doug
Sep 7 '11 #6
patjones
931 Expert 512MB
You need to create the tables on the SQL Server end, link them as I indicated previously, then INSERT the data from your current local tables into the SQL Server linked tables. For the rest of your question, this is the point where I need to understand better what you're trying to do with your code. There is a lot of clarification you need to do. In particular:

1. Where in the code for your first form is the sum of the data "posted to a SQL database..."? I see where the second form is opened (line 13), but where is the data being posted to SQL?

2. With the code in your second form, I am not clear at all on what you're trying to do. You're defining a SQL UPDATE query in the string and then trying to use methods like .AddNew and .Edit on it, which makes no sense. Instead, you should be SELECTing the records you want to work with in the string.

3. What are you doing with 'batchID'. You assign it then do nothing with it.
Sep 7 '11 #7
dougancil
347 100+
Zepp,

The posting of the data to SQL is where I was lost. I'm new to writing in VBA and have no idea how to post that data to sql. As far as the SQL update part, that code was written for me and the requirements have since changed. That was written with the assumption that a record would not be present, but I realized that would only be true once. Lastly, with the batchId, that's supposed to be the frame value from that form, so if a user selects 1, 2 or 3, that's the batchid. I hope that clarifies things a bit more.

I think I'm going to use this method to post my data BUT a question comes up that says that I want to append this data every time and not simply overwrite it.

Expand|Select|Wrap|Line Numbers
  1. DoCmd.TransferDatabase acLink, "ODBC Database", _
  2.  "ODBC;Driver={SQL Server};Server=xxxxx;Database=MDR;" _
  3.  & "Uid=xxxxx;Pwd=xxxxx", acTable, "Payroll", "6_1_Execupay"
how would I go about doing that?
Sep 7 '11 #8
patjones
931 Expert 512MB
To be honest, I still can't make sense of this. What are your tables? What are your controls? What data are you trying to put in the tables?

For example, you need to say "I have text boxes named txt1, txt2, and txt3 whose values I need to add together and insert the result into a table named tblPay when the user hits command button cmdSum" and make it clear how your code is attempting to do those things.

The effectiveness of this forum is based upon people asking clear questions in very concrete terms. You're making vague references to various characteristics of your project, and I can't see how it fits together.

Like with batchID...my question still stands. What are you doing with it? You assign it a value and then end the subroutine without using it anywhere.

Please try and put together the pieces of your project in a way that will make it possible for us to answer the question.
Sep 7 '11 #9
NeoPa
32,556 Expert Mod 16PB
DougAncil:
The Main form is the front end and the second form is the backend
The question was about which database type (Access or SQL Server) was which, but never mind. Pat seems to be dealing with it anyway and if he can understand what you're saying that's all that's important.

@Pat
PS I think we just cross-posted. Good luck with this. I think I've said all I can that might help.
Sep 7 '11 #10

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

Similar topics

18
by: Bryan via AccessMonster.com | last post by:
I am trying to link two separate databases using ms access. I have one database that is a sales log inventory, and another that is a customer data base. I would like to add a command button on my...
1
by: Bryan via AccessMonster.com | last post by:
Private Sub ShipmentRequest_Click() Dim DB As Database Dim rst As DAO.Recordset Set DB = DBEngine(0).OpenDatabase("\\Backup\hosted files\CustomerDatabase\ customerdbnew.mdb") Set rst =...
1
by: That Guy via AccessMonster.com | last post by:
I am trying to link to tables in a database. I have used the following code, but I get the following error Run-time error '-2147467259 (80004005)': Method 'RecordSet' of Object '_Form_Sales...
0
by: Arda | last post by:
Hi all, I tried to connect a database(SQL) by both VS 2003 interface tools and normal typing. When I make the connection in local Web project it doesn't work and gives an error like this; ...
3
by: Makarand Keer | last post by:
Hi all Gurus I have asp.net application where I am creating background process using Threading. My threading involves invoking method which in turn gets some data from SQL server using Windows...
4
by: farhan | last post by:
Please let me know code used to simply connect a database and then add, delete, update records in asp.net using vb.net.
0
by: spiewak | last post by:
Is this possible to connect do the Database Engine (on sql server 2005 on XP platorm - file *.mdf (not mobile *.sdf)) form win CE 5.0 ? I tried to do this ConnectionStringSQLServerCE = "Data...
4
by: ekcunha | last post by:
Hi Forum- I am trying (my hardest) to connect an ACT database (DBF) to an ASP I created. I am totally new to the process - and it all seems to look right - but it won't link in and drop names on...
10
by: Soulspike | last post by:
Hello all again, I am looking for some recommendations from the experts. I have a database that tracks failure information a many different sites. I need all of these satalite datbases to...
0
by: ezrockgw | last post by:
i want to make an application in j2me so that it interacts with database and keeps updating it. How can i connect my mobile to the pc which has the database?...Will it be through bluetooth which i...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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?
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...

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.