By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,222 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,222 IT Pros & Developers. It's quick & easy.

Inserting date into a date unbound table with date datatype

P: 16
I am trying to log my users action into a back-end table if a employee master data is change. Below is my code but i am getting an error with inserting a date.

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2.  
  3. strSQL = "INSERT INTO TblMasterBackEnd " & _
  4.     "( HRAPSNO, AlphaCode, FirstName, LastName, " & _
  5.     "Branch, Dept, Loc, CostCtr, Netuser, " & _
  6.     "DateModified, DateStarted, PayRate, " & _
  7.     "AnnualSalary, BirthDate, Sex, MaritalStatus, " & _
  8.     "Dep, Position, DateCreated) " & _
  9.     "VALUES ('" & Me.txtHRAPSNo & "', '" & _
  10.     Me.txtAlphaCode & "','" & Me.txtFirstName & "','" & _
  11.     Me.txtLastName & "','" & Me.txtBranch & "','" & _
  12.     Me.txtDept & "', '" & Me.txtLoc & "', '" & _
  13.     Me.txtCostCtr & "', '" & Me.txtNetUser & "', #" & _
  14.     Me.txtDateModified & "#, #" & Me.txtDateStarted & "#, " & _
  15.     Me.txtPayRate & ", " & Me.txtAnnualSalary & ", #" & _
  16.     Me.txtBirthDate & "# ,'" & Me.cboSex & "', '" & _
  17.     Me.cboMaritalStatus & "'," & Me.txtDep & ",'" & _
  18.     Me.txtPosition & "', #" & Me.txtDatecreated & "# );"
  19.  
  20. DoCmd.RunSQL (strSQL)
  21. DoCmd.Save
  22. Me.Requery
Apr 26 '18 #1
Share this Question
Share on Google+
3 Replies


twinnyfo
Expert Mod 2.5K+
P: 3,212
Adict,

First, please use the code tags when posting code, which is a rquirement of this forum. I have edited your post to reflect this. Also, concerning this, it is also wise to add line continuation marks on the forum so that users can view your code more easily, without scrolling all the way to the right.

Second, is this a direct cut and paste from your VBA editor? I have made some minor adjustments to the spacing of your code, but since I am not sure this was a direct cut and paste, I cannot say that this would solve anything. On the surface, I don't see anything that pos out concerning your dates.

Third, when you have trouble with SQL statements, it is often useful to have teh code print it to your Immediate Window so you can see exactly what your SQL statement contains before you execute it. Just add the statement Debug.Print strSQL to the line just prior to running your SQL statement (in this case, line 19).

Fourth, if you are inserting all these fields into a table (and it appears these values are currently on a form, is there a particular reason this form is not bound to TblMasterBackEnd? There may be a good reason for this, but it would be good to know.

Fifth, an alternate method (and one that I prefer) is to use a Recordset where you are directly updating the table--this is all preference, but for me, it allows me to more directly see what I am updating.

Sixth, you say you are getting an error with inserting a date. How do you know? What error are you getting and how do you know it is related to the date? This information will help us understand the nature ofyour problem.

Seventh, on the surface, it appears that you are creating redundant data in your Database, which most of us try to avoid unless it is absolutelye necessary. Based on the data listed, I don't see any fields that would much change over time, other than pay rate. Why not just add the index for the employee into the table and only the data that needs to be saved? Again, there could be a good reason for you saving so much information redundantly, but it would be good to know the reasons.

Thanks!
Apr 26 '18 #2

NeoPa
Expert Mod 15k+
P: 31,419
Dates won't work when added that way into a SQL string unless you happen to live and work in North America (at least have your PC settings set that way).

See Literal DateTimes and Their Delimiters (#) for more on that, and How to Debug SQL String for general help when trying to work directly in SQL from VBA.
Apr 26 '18 #3

P: 16
Thanks twinnyfo and Neopa. I have used recordset and achieved the results. The reason why i want a redundant data is to ensure that every data changes users make on the main master file. This is to achieve audit trails for master data.

Thanks much
Apr 27 '18 #4

Post your reply

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