Results 1 to 10 of 10
  1. #1
    Hardhat4u is offline Novice
    Windows 7 64bit Access 2002
    Join Date
    Dec 2013
    Location
    Sacramento, CA by way of Boston, MA
    Posts
    8

    Converting from Access db to SQL Server

    Merry Christmas,

    I am try to convert my VB code which use to connect to an MS Access database (V 2.0) and now is an SQL Server db. I know little about it but I've been reading. I seem to have connected properly to the SQL Database and I'm trying to simply confirm I can see the records of a query I wrote that connects to one of the table in the sql db. Here's what I have....

    Dim mydatabase As Database
    Dim myquerydef As QueryDef
    Dim SPTQueryName As String
    Dim SQLString As String
    Dim ConnectString As String

    Set mydatabase = DBEngine.Workspaces(0).Databases(0)
    Set myquerydef = mydatabase.CreateQueryDef(SPTQueryName)

    ConnectString = "ODBC;DSN=TCMSQL;Description=TCMSQL;UID=TCMUSER;PW D=lyssaeric;DATABASE=TCM;Network=DBMSSOCN"
    SQLString = "SELECT JCJob.JobStructure1, JCJob.Name FROM JCJob ORDER BY JCJob.JobStructure1 aSC;"

    myquerydef.Connect = ConnectString
    myquerydef.SQL = SQLString

    "I want to verify I can return records by simply having the first record come up in a message box. It does not see the SQLString when I type:

    MsgBox myquerydef![JobStructure1]


    myquerydef.Close

    It does not see the pass-through query I know but why? Seem like I have to define some recordset.



    Steve

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Well for starters, you can't get the value that way. Simplest might be a DLookup():

    MsgBox DLookup("JobStructure1", "myquerydef")

    I'd put that after closing the QueryDef to make sure it's saved. If the table is linked, you don't need a pass through query. With a pass through, I wouldn't create a new one, I'd modify the SQL of an existing one.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    By the way, you never set SPTQueryName, and it's the name you'd want to use in the DLookup.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    Hardhat4u is offline Novice
    Windows 7 64bit Access 2002
    Join Date
    Dec 2013
    Location
    Sacramento, CA by way of Boston, MA
    Posts
    8
    Thank you...

    so if I simply give it a name, that will do it?

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Well, no. Like I said, you can't refer to the query the way you did in the message box.

    Just noticed you're in Sac. I was raised there, and my mom still lives there. I'm down there fairly often.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Hardhat4u is offline Novice
    Windows 7 64bit Access 2002
    Join Date
    Dec 2013
    Location
    Sacramento, CA by way of Boston, MA
    Posts
    8
    Okay got it. I've only been here 19 months. Lived all but 2 out of my soon-to-be 62 years outside of Boston. I like it here but miss the kids and grandkids. I just got back from the east coast for an early holiday.

  7. #7
    Hardhat4u is offline Novice
    Windows 7 64bit Access 2002
    Join Date
    Dec 2013
    Location
    Sacramento, CA by way of Boston, MA
    Posts
    8
    pbaldy,

    I finally did get what I want but trying a different approach now by attaching an SQL table called APVendor and trying to filter a record from it like I did in access thousands of times. Here's the problem. When I write a query in VB using the attached sql table, it blows right by the criteria if I set the criteria as a variable. Here's my code...

    Dim db As Database
    Dim ssJob As Recordset
    Dim sqlJob, VId As String

    Set db = CurrentDb()

    VId = CStr(Me![Text2])
    sqlJob = "SELECT dbo_APVendor.Vendor, dbo_APVendor.Name FROM dbo_APVendor WHERE (((dbo_APVendor.Vendor) = "" & VId & "") And ((dbo_APVendor.CompanyId) = ""1"")) ORDER BY dbo_APVendor.Vendor, dbo_APVendor.Name;"
    Set ssJob = db.OpenRecordset(sqlJob, dbOpenSnapshot)


    MsgBox ssJob![Name]

    "The error message that gets returned is Too few parameters, expected 1."

    If I use that same code using an access db, it works perfectly.

    Obviously, writing a query in VBA using a SQL table reacts differently.

    Any help?

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try:

    sqlJob = "SELECT dbo_APVendor.Vendor, dbo_APVendor.Name FROM dbo_APVendor WHERE dbo_APVendor.Vendor = '" & VId & "' And dbo_APVendor.CompanyId = '1' ORDER BY dbo_APVendor.Vendor, dbo_APVendor.Name;"

    If either field in the criteria has a numeric data type, remove the single quotes around it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Hardhat4u is offline Novice
    Windows 7 64bit Access 2002
    Join Date
    Dec 2013
    Location
    Sacramento, CA by way of Boston, MA
    Posts
    8
    Paul, that did it. I really appreciate the help you've given me. This opens up many more doors.

    Hope you had a great holiday.

    S

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help Steve!

    Had a quiet holiday; Thanksgiving was the hectic one with 20+ family in town. Hope you had a great one too!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 3
    Last Post: 07-13-2013, 01:36 PM
  2. Replies: 7
    Last Post: 05-13-2013, 11:37 AM
  3. help in converting excel to access
    By Daniel2000 in forum Access
    Replies: 11
    Last Post: 04-25-2013, 01:43 AM
  4. Converting Access Backend from Access 2003 to SQL Question
    By seattlebrew in forum Database Design
    Replies: 0
    Last Post: 03-07-2013, 07:14 PM
  5. Replies: 3
    Last Post: 05-23-2010, 05:23 AM

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