Results 1 to 12 of 12
  1. #1
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246

    Trouble mapping 7 Access percentages to SQL Server 2008 r2 using variables in VBA

    Access front end, Sql Server backend, Mismatched data type when appending to SQL Server. These are all LinkedTables.



    I am pulling 7 percentages .25% into dimmed variables in VBA. The variables are included in a SQL Insert Into statement. The SQL statement runs fine except for the percentages. SQL server 2008 r2 fields are:

    This is the Field Type setting in SQL Server:

    Click image for larger version. 

Name:	SQL Screen Shot Decimal.JPG 
Views:	14 
Size:	21.9 KB 
ID:	19766

    Dimmed Variables in VBA statement. I have tried many different types of variables, Number, Varient, etc. Nothing works.

    Dim varFYTFP4 As Variant
    Dim varSYTFP3 As Variant
    Dim varSYTFP4 As Variant
    Dim varTYTF As Variant
    Dim varVPFP5 As Variant
    Dim varCEYEAR As Variant

    This is the dlookup function that grabs the 7 variables from the Access lookup table:

    NumFYTFP3 = Nz(DLookup("FyrTriP3", "dbo_feeScheduleLookup")) 'Tried Number it didn't work.

    varFYTFP4 = Nz(DLookup("FYRTriP4", "dbo_feeScheduleLookup"))
    varSYTFP3 = Nz(DLookup("SyrTriP3", "dbo_feeScheduleLookup"))
    varSYTFP4 = Nz(DLookup("SyrTriP4", "dbo_feeScheduleLookup"))
    varTYTF = Nz(DLookup("TyrTriFee", "dbo_feeScheduleLookup"))
    varVPFP5 = Nz(DLookup("VOPtabFee5", "dbo_feeScheduleLookup"))
    varCEYEAR = Nz(DLookup("CEyear", "dbo_feeScheduleLookup"))

    This is the SQL statement the variables are in the last line of the VALUES.

    Fields and variables are highlighted in Bold and Text size.

    Code:
     
     
                stSQL2 = "INSERT INTO DBO_PropertyYearDetail" & _
                    "( PropertyID, County, TownshipNM, TownshipNO, kPIN, CycleID, CycleName, LevelofAssess, kAttyAssigned, FirstYRTriFeeP3, FirstYRTriFeeP4, SecondYrTriFeeP3, SecondYrTriFeeP4, ThirdYrTriFee, VOPTABFeeP5, CEYEAR) " _
                    & "VALUES ('" & varX2 & "','" & stCnty & "','" & stSelTwn & "','" & _
                    stTwnNo & "','" & stKPIN & "','" & stCycleID & "','" & stCycleNM & _
                    "','" & stLOA & "','" & stKattAs & _
                    "',' & NumFYTFP3 & ',' & varFYTFP4 & ',' & varSYTFP3 & ',' & varSYTFP4 & ',' & varTYTF & ',' & varVPFP5 & ', ' & varCEYEAR & ');"
    
               DoCmd.RunSQL stSQL2
    You will see different Dim types and different variables in the SQL statement. That is from my various tries, please ignore.

    I cannot find the correct data types in Access VBA that will successfully write to the Sql Table. So far every type has failed.

    What variable in the Access VBA statement works with Decimal(2.2) in SQL Server?

    Any suggestions?

    Fred

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,895
    Variables should not be within quote marks. Replace the apostrophes with quote marks.

    "'," & NumFYTFP3 & "," & varFYTFP4 & "," & varSYTFP3 & "," & varSYTFP4 & "," & varTYTF & "," & varVPFP5 & ", " & varCEYEAR & ");"

    Should probably specify the alternate value in the Nz() function.

    varFYTFP4 = Nz(DLookup("FYRTriP4", "dbo_feeScheduleLookup"), 0)

    Is there only one record in the lookup table?

    Also, number type fields should not have apostrophe delimiters. Is PropertyID a number type?

    Why are you saving CycleID and CycleName?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246

    Continuing problem with dates.

    I followed all of your recommendations and it cured a lot of problems. I have one problem left. I can't get the dates to append to the tables in the queries. There are so many places the problem could be. Here is what I have:

    There are lots of date fields coming up. The two dates/fields I am having an immediate problem with are:

    TaxYear

    OpenFile

    This is the SQL table with the TaxYear field created as "Date".

    Click image for larger version. 

Name:	Capture.JPG 
Views:	9 
Size:	71.2 KB 
ID:	19793

    Here is a screen shot of the Add New Property record.

    Click image for larger version. 

Name:	Screen design.JPG 
Views:	9 
Size:	39.2 KB 
ID:	19794

    Here are the date field's property settings.

    Click image for larger version. 

Name:	Properties.JPG 
Views:	9 
Size:	27.4 KB 
ID:	19795

    Here are the DIM statements for the dates:

    Dim dtTaxYear As Date

    Dim dtOpenFile As Date

    dtOpenFile = Me.OpenFile

    dtTaxYear = Me.TaxYear

    , #" & dtTaxYear & "#,

    And here is the SQL statement:

    Code:
    stsql1 = "INSERT INTO dbo_Property" & _
          "( County, kPIN, TownshipNO, TownshipNM, CycleID, CycleName, CountyClass, PropertyDesc, LevelofAssess, Type, TaxYear, OpenFile, KattyAssigned, TaxCode)" _
          & "VALUES ('" & stCnty & "','" & stKPIN & "','" & stTwnNo & "','" & stSelTwn _
          & "','" & stCycleID & "','" & stCycleNM & "','" & stSelClas & "','" & stDesc _
          & "','" & stTypeNM & "','" & stLOA & "',#" & dtTaxYear & "#, #" & dtOpenFile _
          & "#, '" & stKattAs & "', '" & stTaxCode & "');"
    The SQL date field is the type that shows the date without the time. Used in other places, like from data entry on screens, the date writes fine to the underlying table.

    Thanks so much for your time.

    Fred

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,895
    Don't see anything wrong with syntax - # delimiter always works for me but I have never used SQL Server as backend.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246
    Thanks for all your help. I will wait to see if there are any more thoughts.

    Syntax for SQL Server VBA date format.

    Fred

  6. #6
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    if you are appending to a SQL table from Access you have to use ' markers just like it was text. at least that's always been my experience.

  7. #7
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246
    What are markers?

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,895
    rpeare means apostrophe (') delimiters - just as you have for the other parameters in the SQL statement.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246
    like, '# & dtYear & #',

    Or

    like, #' & dtyear & '#, ??

    Would the marker be considered a wrapper?

    Thanks

  10. #10
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    no, # is an invalid marker in SQL (as far as I know)

    ... ",' " & dtTaxYear & " ' , ' " & dtOpenFile & " ', " ...

    just make sure to remove the spaces I have put in for visibility.

  11. #11
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246
    Thanks both. I will give this a try. It just looks like a text string, just dimmed as a Date. I will report back on my trial so others will know.

    Fred

  12. #12
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246
    rpeare and june7, thanks that did it. I had to drop the hashtag # and dim the date as a string:

    SQL Server 2008 R2
    SQL Insert Into statement
    SQL Date field selection standard "Date".

    dim stTaxYear as string
    dim stOpenFile as string

    '" & stTaxCode & "'
    '" & stOpenFile & "'

    Thanks for all your help.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Access 2010 to SQL Server 2008 R2
    By bigroo in forum SQL Server
    Replies: 0
    Last Post: 03-21-2013, 07:02 PM
  2. Access 2010 through VPN and Win Server 2008
    By feguillen in forum Misc
    Replies: 1
    Last Post: 12-01-2011, 06:20 PM
  3. Replies: 1
    Last Post: 04-21-2011, 07:12 PM
  4. Can't connect Access 2010 to SQL Server 2008 R2
    By LAazsx in forum Import/Export Data
    Replies: 6
    Last Post: 12-10-2010, 08:44 PM
  5. .adp in access 2007 to SQl server 2008
    By NoellaG in forum Access
    Replies: 5
    Last Post: 09-07-2010, 09:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums