Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 21st, 2005, 05:49 PM
Registered User
 
Join Date: Feb 2005
Location: Green Bay, WI, USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Trouble with comparing date fields

I have a program (code is below) which writes records to a table only if a record with matching keys doesn't already exist. The problem is, when I compare the date field, it never matches, so a new record gets written every time, even if a matching record exists. I have a SQL Server backend and am working with Access 2003 on Small Business Server 2003. Does anyone have a suggestion?

Private Function Write_Prod_Run() As Boolean
On Error GoTo Err_Write_Prod_Run

  Write_Prod_Run = False

  strRcdSrc = "Select * From ProductionRuns " & _
  "Where ProdID = '" & txtProdID & "' and MachID = '" & txtMachID & _
  "' and EmpID = '" & txtEmpID & "' and ProdDate = " & _
  Format(txtProdDate, mmddyyyy) & " and Shift = " & frmShift & ";"

  rstProdRun.Open strRcdSrc, cnn, adOpenDynamic, adLockOptimistic

  With rstProdRun
    If .EOF = True Then 'Record not found, insert new record.
      .AddNew
      .Fields("ProdID") = txtProdID
      .Fields("MachID") = txtMachID
      .Fields("EmpID") = txtEmpID
      .Fields("ProdDate") = Format(txtProdDate, mmddyyyy)
      .Fields("Shift") = frmShift
      .Update
    End If
  End With

  Write_Prod_Run = True

Exit_Write_Prod_Run:
  rstProdRun.Close
  Exit Function

Err_Write_Prod_Run:
  MsgBox Err.Description
  Resume Exit_Write_Prod_Run
End Function


Reply With Quote
  #2 (permalink)  
Old February 21st, 2005, 08:51 PM
Authorized User
 
Join Date: Jul 2004
Location: clapton-in-gordano, n.somerset, United Kingdom.
Posts: 46
Thanks: 0
Thanked 1 Time in 1 Post
Default


First thing that struck me was that if [ProdDate] is defined as a Date/Time field you need date littorals (#) around the date e.g.

ProdDate = #" & _
  Format(txtProdDate, mmddyyyy) & "# ..... etc

SQL also insists on dates in US format, but you seem to have that covered

Cheers

Alan t





Reply With Quote
  #3 (permalink)  
Old February 21st, 2005, 09:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi Jimmy and Alan,

Alan, you missed something I've missed a bunch before too, but Jimmy is using SQL Server as a backend, not Access.

So Jimmy, the date parameter you're passing to SQL Server is a string literal, and will therefore need to be delimited by apostrophes also. The following will match your date field for you:

strRcdSrc = "Select * From ProductionRuns " & _
  "Where ProdID = '" & txtProdID & "' and MachID = '" & txtMachID & _
  "' and EmpID = '" & txtEmpID & "' and ProdDate = '" & _
  Format(txtProdDate, mmddyyyy) & "'"

This will produce a SQL statement like:

Select *
From ProductionRuns
Where ProdID = '1'
and MachID = '1'
and EmpID = '1'
and ProdDate = '1/1/2001'

Notice that the date is delimited by apostrophes also.

HTH,

Bob

Reply With Quote
  #4 (permalink)  
Old February 22nd, 2005, 09:14 AM
Registered User
 
Join Date: Feb 2005
Location: Green Bay, WI, USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Bob! The quotes did the trick. I had tried enclosing the date with #s, but SQL Server doesn't like it.

:D

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing date column with sql server mallikalapati ASP.NET 2.0 Professional 3 March 12th, 2008 06:06 PM
comparing date/time fields on form validation Lucy Javascript How-To 1 June 20th, 2006 09:22 AM
Trouble with date formats DrewMills Access VBA 3 March 7th, 2005 06:22 PM
comparing CLOB fields pankaj_daga Access 0 August 5th, 2004 04:38 AM
prob with comparing date in access nilesh_parmar Access 1 March 29th, 2004 10:59 AM



All times are GMT -4. The time now is 02:29 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.