Results 1 to 27 of 27

Thread: Access Module Code: rst.movenext I assume... (Resolved in the nick of time)

  1. #1

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    Access Module Code: rst.movenext I assume... (Resolved in the nick of time)

    Presumably it is perfectly simple to open an SQL statement OR a Query and then move through it record by record until EOF thus doing something with each value.

    In this case I shall be wanting to do stuff to a word document dependent on the data.

    My question just boils down to this:

    I understand that I will be using this structure

    If not EOF then
    Do
    Open the SQL/Query (somehow) into some object or other
    result = function_to_do_stuff(object.rst.field("foo"))
    result = function_to_do_stuff(object.rst.field("bar"))
    object.rst.movenext
    loop untill EOF
    end if

    BUT what syntax, words, objects etc am I to use... basically I have no idea of the correct use of the language in this instance.

    Could someone post some actual code in order to get me going in the right direction?

    BTW: the code will run in a module

    [edit]Or a pointer to an article or anything just need to start[/edit]
    Last edited by Matt_T_hat; Sep 19th, 2003 at 05:44 AM.
    ?
    'What's this bit for anyway?
    For Jono

  2. #2

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    here for example is a sample I picked up a while ago:

    VB Code:
    1. doc.Bookmarks("OrderDetails").Select
    2. Do
    3.     With objWord.Selection
    4.         .TypeText vbTab
    5.         .TypeText rstOrder.Fields("Quantity")
    6.         .TypeText vbTab
    7.         .TypeText rstOrder.Fields("ProductName")
    8.         .TypeParagraph
    9.     End With
    10.     rstOrder.MoveNext
    11. Loop While Not (rstOrder.EOF)

    this was part of a bookmarks useage tutorial but the writter assumed that the user would know how to Dim and initialise the object... but I don't know.
    ?
    'What's this bit for anyway?
    For Jono

  3. #3
    Addicted Member
    Join Date
    Aug 2003
    Location
    houston
    Posts
    185
    this is an example it will not work without database name and proper sql statement
    i am assuming you are using access


    Public cn As New ADODB.Connection
    Public rstOrder As New ADODB.Recordset


    Private Sub cmdOpenRS_Click()

    On Error GoTo AdoError


    Dim strConnect As String
    Screen.MousePointer = vbHourglass
    'place your database here
    'Check to be sure the connection is not already opened:
    Path = "C:\orders.mdb"
    If cn.State = 1 Then
    cn.Close
    Set cn = Nothing
    End If
    strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Path & ";"
    ' Set Conn = Server.CreateObject("ADODB.Connection")

    cn.Open strConnect

    'Check to be sure a Recordset is not already opened:
    If rstOrder.State = 1 Then
    rstOrder.Close
    Set rstOrder = Nothing
    End If

    With rstOrder
    .CursorLocation = adUseClient
    .CursorType = adOpenStatic
    .LockType = adLockOptimistic
    .CacheSize = 50
    .Source = "Select * from yourtablename"
    .ActiveConnection = cn
    .Open
    End With



    doc.Bookmarks("OrderDetails").Select
    Do
    With objWord.Selection
    .TypeText vbTab
    .TypeText rstOrder.Fields("Quantity")
    .TypeText vbTab
    .TypeText rstOrder.Fields("ProductName")
    .TypeParagraph
    End With
    rstOrder.MoveNext
    Loop While Not (rstOrder.EOF)


    rstOrder.Close
    Set rstOrder = Nothing
    cn.Close
    Set cn = Nothing
    Screen.MousePointer = vbNormal

    Exit Sub

    AdoError:

    Dim errLoop As Error
    Dim strError As String

    myI = 1

    StrTmp = StrTmp & vbCrLf & "VB Error # " & Str(Err.Number)
    StrTmp = StrTmp & vbCrLf & " Generated by " & Err.Source
    StrTmp = StrTmp & vbCrLf & " Description " & Err.Description

    ' Display Error Information for each Error object.
    Set Errs1 = cn.Errors
    For Each errLoop In Errs1
    With errLoop
    StrTmp = StrTmp & vbCrLf & "Error #" & myI & ":"
    StrTmp = StrTmp & vbCrLf & " ADO Error #" & .Number
    StrTmp = StrTmp & vbCrLf & " Description " & .Description
    StrTmp = StrTmp & vbCrLf & " Source " & .Source
    myI = myI + 1
    End With
    Next

    MsgBox StrTmp
    rstOrder.Close
    Set rstOrder = Nothing
    cn.Close
    Set cn = Nothing

    End Sub
    '******add a reference to ado in project

  4. #4

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    Brillient you have saved me from death at 1200 (deadline time) I will post here again when I have tried out the code you have posted but in the mean time - T H A N K Y O U !
    ?
    'What's this bit for anyway?
    For Jono

  5. #5

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    Originally posted by Matt_T_hat
    Brillient you have saved me from death at 1200 (deadline time) I will post here again when I have tried out the code you have posted but in the mean time - T H A N K Y O U !
    or possibly mostly thankyou... I forgot to mention that this would be running in a module in Access itself... Should still work though I guess...

    one would assume that
    VB Code:
    1. 'place your database here
    2. 'Check to be sure the connection is not already opened:
    3. Path = "C:\orders.mdb"
    4. If cn.State = 1 Then
    5. cn.Close
    6. Set cn = Nothing
    7. End If
    8. strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Path & ";"
    9. ' Set Conn = Server.CreateObject("ADODB.Connection")
    should be left out...
    ?
    'What's this bit for anyway?
    For Jono

  6. #6
    Addicted Member
    Join Date
    Aug 2003
    Location
    houston
    Posts
    185
    inside of access you can use DAO add a reference to the project
    just inccase you have problem with ado
    Private Sub cmdOpenRS_Click()

    On Error GoTo AdoError
    Dim strSQL As String

    Dim rstOrder As New DAO.Recordset
    Dim dbProducts As New DAO.Database



    Screen.MousePointer = vbHourglass
    strSQL = "Select * from orders"
    Set dbProducts = CurrentDb
    Set rstOrder = dbProducts.OpenRecordset(strSQL, dbOpenSnapshot)

    doc.Bookmarks("OrderDetails").Select
    Do
    With objWord.Selection
    .TypeText vbTab
    .TypeText rstOrder.Fields("Quantity")
    .TypeText vbTab
    .TypeText rstOrder.Fields("ProductName")
    .TypeParagraph
    End With
    rstOrder.MoveNext
    Loop While Not (rstOrder.EOF)


    rstOrder.Close
    Set rstOrder = Nothing
    dbProducts.Close
    Set dbProducts = Nothing
    Screen.MousePointer = vbNormal

    Exit Sub

    AdoError:

    Dim errLoop As Error
    Dim strError As String

    myI = 1

    StrTmp = StrTmp & vbCrLf & "VB Error # " & Str(Err.Number)
    StrTmp = StrTmp & vbCrLf & " Generated by " & Err.Source
    StrTmp = StrTmp & vbCrLf & " Description " & Err.Description

    ' Display Error Information for each Error object.
    Set Errs1 = cn.Errors
    For Each errLoop In Errs1
    With errLoop
    StrTmp = StrTmp & vbCrLf & "Error #" & myI & ":"
    StrTmp = StrTmp & vbCrLf & " Dao Error #" & .Number
    StrTmp = StrTmp & vbCrLf & " Description " & .Description
    StrTmp = StrTmp & vbCrLf & " Source " & .Source
    myI = myI + 1
    End With
    Next

    MsgBox StrTmp
    rstOrder.Close
    Set rstOrder = Nothing
    dbProducts.Close
    Set dbProducts = Nothing



    End Sub

  7. #7

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    Now that is good. I've just come to say that the modified version I have made fails and behold you have posted an inside access version

    Nice one thanks.

    BTW: for those without time-telling-devices I am still working past the dead line so I am extra thankfull for every moment I save.
    Last edited by Matt_T_hat; Sep 1st, 2003 at 11:00 AM.
    ?
    'What's this bit for anyway?
    For Jono

  8. #8
    Addicted Member
    Join Date
    Aug 2003
    Location
    houston
    Posts
    185

    Wink

    you're welcome
    you still have to get the word object
    i didnt do that

  9. #9
    Addicted Member
    Join Date
    Aug 2003
    Location
    houston
    Posts
    185
    oops i forgot to fix the error statement
    just remove the cn,errors statement s

  10. #10

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    I get Invalid Use of New Keyword for:

    VB Code:
    1. Dim rstOrder As New DAO.Recordset

    I feel I am 99% there......
    ?
    'What's this bit for anyway?
    For Jono

  11. #11
    Addicted Member
    Join Date
    Aug 2003
    Location
    houston
    Posts
    185
    take off new it doesnt like it i guess
    it will work then

    as long as the word object is initialized and you remmed out the error statements
    ' Display Error Information for each Error object.
    'Set Errs1 = cn.Errors
    'For Each errLoop In Errs1
    'With errLoop
    'StrTmp = StrTmp & vbCrLf & "Error #" & myI & ":"
    'StrTmp = StrTmp & vbCrLf & " Dao Error #" & .Number
    'StrTmp = StrTmp & vbCrLf & " Description " & .Description
    'StrTmp = StrTmp & vbCrLf & " Source " & .Source
    'myI = myI + 1
    'End With
    'Next

  12. #12

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    Originally posted by adocwra
    take off new it doesnt like it i guess
    it will work then
    I'd already removed the New from both lines and it worked with that fine, whoever
    VB Code:
    1. Set rstOrder = dbProducts.OpenRecordset(strSQL, dbOpenSnapshot)
    always results in error handleing being called.
    VB Code:
    1. strSQL = "[WORD_Short]"
    gives the name of a query... but it says it can not find the thing... hmm says I.

    Thoughts?

    BTW: we have mannaged to get far enough with everything else to buy further deadline grace.
    ?
    'What's this bit for anyway?
    For Jono

  13. #13
    Addicted Member
    Join Date
    Aug 2003
    Location
    houston
    Posts
    185
    are you using a query or sql statement

  14. #14
    Addicted Member
    Join Date
    Aug 2003
    Location
    houston
    Posts
    185
    Dim qry As QueryDef
    Set dbProducts = CurrentDb
    Set qry = dbProducts.QueryDefs("qrycust")
    this opens up a query

  15. #15

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    Originally posted by adocwra
    Dim qry As QueryDef
    Set dbProducts = CurrentDb
    Set qry = dbProducts.QueryDefs("qrycust")
    this opens up a query
    Ok... right my mistake thanks.

    I'll let you know how this pan's out
    ?
    'What's this bit for anyway?
    For Jono

  16. #16

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    It's going better

    but I now face a new problem

    VB Code:
    1. Private Function DoLoop(BM_Loco As String, LongVer As Boolean) As Boolean
    2. Dim strSQL As String
    3. Dim rstOrder As QueryDef
    4. Dim dbProducts As DAO.database
    5. Dim ThisIsIt
    6. Dim DoResult As Boolean
    7.  
    8. On Error GoTo AdoError
    9. Screen.MousePointer = vbHourglass
    10. DoResult = False
    11.  
    12.      
    13.     'The Long Version
    14.     '================
    15.       If LongVer = True Then
    16.             strSQL = "[WORD_Long]"
    17.             Set dbProducts = CurrentDb
    18.             Set rstOrder = dbProducts.QueryDefs(strSQL)
    19.             Do
    20.                 CopyThis rstOrder.Fields("Long_Note")
    21.                 PasteThis BM_Loco
    22.                 rstOrder.MoveNext
    23.             Loop While Not (rstOrder.EOF)
    24.             DoResult = True
    25.       Else
    26. 'Etc....

    I am told that rstorder.movenext is "method or data member not found". The problem is I am now about as far out on a limb as I can get and tredding water as far as ability to understand what I am dealing with. I have little doubt as to my ability to get there sooner or later but I fear it could be later...

    I guess this is where I am told "Duh - You cant **** you have to #### instead if you want to $$$$$$$"

    Sigh.

    I think I do not undertand these objects all that well.
    ?
    'What's this bit for anyway?
    For Jono

  17. #17

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    next I tried

    VB Code:
    1. If Not (rstOrder.EOF) Then rstOrder.MoveNext
    this leads me to think that I am using the wrong language to deal with my open query as it objected to the EOF in the If Not...
    ?
    'What's this bit for anyway?
    For Jono

  18. #18
    Addicted Member
    Join Date
    Aug 2003
    Location
    houston
    Posts
    185
    i forget to tell you to add the querydef to a recordset
    [code]
    Dim qry As QueryDef
    Dim rst As Recordset

    Set qry = db.QueryDefs(strSQL)
    'if you have to pass params
    ' qry.Parameters("pStartDt") = fStartDt
    ' qry.Parameters("pEndDt") = fEndDt
    Set rst = qry.OpenRecordset(dbOpenSnapshot, dbReadOnly)

    Do
    CopyThis rst.Fields("Long_Note")
    PasteThis BM_Loco
    rst.MoveNext
    Loop While Not (rst.EOF)
    DoResult = True

  19. #19

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    It all looks so shokingly easy when someone explains it.



    Thankyou.
    ?
    'What's this bit for anyway?
    For Jono

  20. #20

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    This is where I am up to

    VB Code:
    1. Private Function DoLoop(BM_Loco As String, LongVer As Boolean) As Boolean
    2. Dim strSQL As String
    3. Dim qry As QueryDef
    4. Dim rstOrder As Recordset 'QueryDef
    5. Dim dbProducts As DAO.database
    6. Dim ThisIsIt
    7. Dim DoResult As Boolean
    8.  
    9. On Error GoTo AdoError
    10. Screen.MousePointer = vbHourglass
    11. DoResult = False
    12.      
    13.          If LongVer = True Then
    14.            'Do long stuff with word
    15.         Else
    16.       'The Short Version
    17.             strSQL = "[WORD_Short]"
    18.             Set qry = db.QueryDefs(strSQL)
    19.             Set dbProducts = CurrentDb
    20.             Set rstOrder = qry.OpenRecordset(dbOpenSnapshot, dbReadOnly)
    21.             Do While Not (rstOrder.EOF)
    22.                 ThisIsIt = rstOrder.Fields("Name") & vbTab & _
    23.                 rstOrder.Fields("Price") & " + VAT at 17.5%"
    24.                 TypeThis ThisIsIt, BM_Loco
    25.                 rstOrder.MoveNext
    26.             Loop
    27.             DoResult = True
    28.       End If
    29.  
    30.     rstOrder.Close
    31.     Set rstOrder = Nothing
    32.     cn.Close
    33.     Set cn = Nothing
    34.    
    35.     Screen.MousePointer = vbNormal
    36.  
    37. GoTo exitme         'skip the error block
    38. AdoError:
    39. Screen.MousePointer = vbNormal
    40. FrErr "Access-to-Word_AutoBM (" & DoResult & ")"
    41. On Error GoTo exitme
    42.     rstOrder.Close
    43.     Set rstOrder = Nothing
    44.     cn.Close
    45.     Set cn = Nothing
    46. exitme:
    47. DoLoop = DoResult
    48. End Function

    I find that this bombs to the on error section which errors and thus quits it.
    VB Code:
    1. Set qry = db.QueryDefs(strSQL)
    ?
    'What's this bit for anyway?
    For Jono

  21. #21

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    I am definitly going to need some tutorials on this subject.

    If you have a moment after spotting where I've cocked up (if I don't guess first) I could do with a little explination.

    EDIT:
    VB Code:
    1. Set dbProducts = CurrentDb
    This looks like it's serplus to requirments.
    Last edited by Matt_T_hat; Sep 18th, 2003 at 09:56 AM.

  22. #22
    Addicted Member
    Join Date
    Aug 2003
    Location
    houston
    Posts
    185
    off the top of my head i see



    Set qry = db.QueryDefs(strSQL)
    Set dbProducts = CurrentDb


    it should be

    Dim db As DAO.database

    set db=currentdb
    then db.querydef(strsql)

    then i see

    strSQL = "[WORD_Short]"

    you dont need square brackets
    i am assuming strsql="Access Query Name"
    strSQL = "WORD_Short"

    then i see
    Dim dbProducts As DAO.database
    and

    Dim qry As QueryDef
    Dim rstOrder As Recordset 'QueryDef
    which should be
    Dim qry As DAO.QueryDef
    Dim rstOrder As DAO.Recordset 'QueryDef
    and you have to make sure you have a reference to DAO

    in tools Reference

    Microsoft DAO xx.x Object Library


    let me look further

  23. #23
    Addicted Member
    Join Date
    Aug 2003
    Location
    houston
    Posts
    185
    try this it compiles so no code problems but I cant run it without spending a whole bunch of time
    let me know


    Code:
    Private Function DoLoop(BM_Loco As String, LongVer As Boolean) As Boolean
    Dim strSQL As String
    Dim qry As DAO.QueryDef
    Dim rstOrder As DAO.Recordset 'QueryDef
    Dim db As DAO.Database '.database
    Dim ThisIsIt
    Dim DoResult As Boolean
    
    On Error GoTo AdoError
    Screen.MousePointer = vbHourglass
    DoResult = False
          
             If LongVer = True Then
               'Do long stuff with word
            Else
          'The Short Version
                strSQL = "WORD_Short"
                Set db = CurrentDb 'CurrentDb
                Set qry = db.QueryDefs(strSQL)
     
                Set rstOrder = qry.OpenRecordset(dbOpenSnapshot, dbReadOnly)
                Do While Not (rstOrder.EOF)
                    ThisIsIt = rstOrder.Fields("Name") & vbTab & _
                    rstOrder.Fields("Price") & " + VAT at 17.5%"
                   ' TypeThis ThisIsIt, BM_Loco
                    rstOrder.MoveNext
                Loop
                DoResult = True
          End If
      
        rstOrder.Close
        Set rstOrder = Nothing
    'not used
    '    cn.Close
    '    Set cn = Nothing
        
        Screen.MousePointer = vbNormal
    
    GoTo exitme         'skip the error block
    AdoError:
    Screen.MousePointer = vbNormal
    'FrErr "Access-to-Word_AutoBM (" & DoResult & ")"
    On Error GoTo exitme
        rstOrder.Close
        Set rstOrder = Nothing
    '    cn.Close
    '    Set cn = Nothing
    exitme:
    DoLoop = DoResult
    End Function

  24. #24

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    Originally posted by adocwra
    try this it compiles so no code problems but I cant run it without spending a whole bunch of time
    let me know
    adocwra go to the top of the class.


    Don't I look stupid. I was useing an inconsistant nameing method and I needed a DAO.Recordset to go with the DAO.database where as I had now "DAO.".

    The DAO I would not have guessed but it seems so obviouse now and the wrong order with wrong named stuff is just shocking.

    I will send myself to stand in the corner!! "F" For you this term Mr Hat!
    ?
    'What's this bit for anyway?
    For Jono

  25. #25
    Addicted Member
    Join Date
    Aug 2003
    Location
    houston
    Posts
    185
    if you dont specify dao and in the reference ADO is checked it will assume ADO

    I had to do a lot of conversions at work and this was the number one problem

  26. #26

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    Originally posted by adocwra
    if you dont specify dao and in the reference ADO is checked it will assume ADO

    I had to do a lot of conversions at work and this was the number one problem
    Perhaps I need to find out a bit more about this subject... like anything for example.
    ?
    'What's this bit for anyway?
    For Jono

  27. #27
    Addicted Member
    Join Date
    Aug 2003
    Location
    houston
    Posts
    185
    i guess ado alphabetically is before dao
    do when you declare a variable to a database and a recordset it will assume ado not dao
    and the ado has different properties and methods than dao
    plus because mdac was installed funky the had to have both for them to work

    we had thousands of spreadsheets all over the network that had problems once they upgraded the workstation mdac

    but luckily its a simple fix if it doesnt use ado remove it else specifically declare the variable as a dao.database /recordset

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width