Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 21st, 2005, 05:49 PM
Registered User
 
Join Date: Feb 2005
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


 
Old February 21st, 2005, 08:51 PM
Authorized User
 
Join Date: Jul 2004
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





 
Old February 21st, 2005, 09:51 PM
Friend of Wrox
 
Join Date: Jun 2003
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

 
Old February 22nd, 2005, 09:14 AM
Registered User
 
Join Date: Feb 2005
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






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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.