Results 1 to 6 of 6
  1. #1
    orcinus is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Apr 2010
    Posts
    67

    Why Cannot Pass A Variable In A 'for I = ' Statement

    In the below code, I cannot seem to concatenate as:

    r!OBJ_RUN = strObjRun & Trim(i)

    so that it will pass the variable value:

    1stObj (from strObjRun1 = "1stObj")

    into the OBJ_RUN field in the tbl_Tracker table.

    Is this even possible? Or am I going to actually have to write each instance out and forget using the for i = 1 to x statement?

    ===============================================
    Public Function fcnRunRptTimed()

    Dim d As DAO.Database
    Dim r As Recordset


    Dim strObjTbl As String
    Dim strSQL As String
    Dim i As Integer

    Dim strObjRun1 As String
    Dim strObjRun2 As String

    strObjRun1 = "1stObj"
    strObjRun2 = "2ndObj"

    strTbl = "tbl_RptTracker"
    strSQL = "select * from " & strTbl

    Set d = CurrentDb
    Set r = d.OpenRecordset(strSQL, dbOpenDynaset)

    For i = 1 To 2

    r.AddNew
    r!ST = Now()
    r!OBJ_RUN = strObjRun & Trim(i)
    r.Update

    Next i
    End Function

  2. #2
    llkhoutx is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Location
    Houston, Texas USA
    Posts
    373
    Try

    r!OBJ_RUN = strObjRun & cstr(i)

  3. #3
    orcinus is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Apr 2010
    Posts
    67
    Quote Originally Posted by llkhoutx View Post
    Try

    r!OBJ_RUN = strObjRun & cstr(i)
    This doesn't work either. I'm thinking this can't be done?

  4. #4
    c_smithwick is offline Underpaid Programmer
    Windows 7 Access 2003
    Join Date
    Jan 2010
    Location
    Lakeside, CA
    Posts
    49
    Try specifically declaring your recordset as a DAO.Recordset. It might not be updatable as declared. You are saying it doesn't work. What specific error are you getting? Or is it simply failing to add the records? Is your OBJ_RUN field defined as a text field?

  5. #5
    llkhoutx is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Location
    Houston, Texas USA
    Posts
    373
    It can be done.

    The following is not correct
    Code:
    strTbl "tbl_RptTracker"
    strSQL "select * from " strTbl

    Set d 
    CurrentDb
    Set r 
    d.OpenRecordset(strSQLdbOpenDynaset
    It should be
    Code:
    strTbl "tbl_RptTracker"
    Set d CurrentDb
    Set r 
    d.OpenRecordset("tbl_RptTracker"dbOpenDynaset
    You've misunderstood the meaning of a recordset.

  6. #6
    c_smithwick is offline Underpaid Programmer
    Windows 7 Access 2003
    Join Date
    Jan 2010
    Location
    Lakeside, CA
    Posts
    49
    Found it:

    Code:
    Public Function fcnRunRptTimed()
    
    Dim d As DAO.Database
    Dim r As Recordset
    Dim strObjTbl As String
    Dim strSQL As String
    Dim i As Integer
    
    Dim strObjRun1 As String
    Dim strObjRun2 As String
    
    strObjRun1 = "1stObj"
    strObjRun2 = "2ndObj"
    
    strTbl = "tbl_RptTracker"
    strSQL = "select * from " & strTbl
    
    Set d = CurrentDb
    Set r = d.OpenRecordset(strSQL, dbOpenDynaset)
    
    For i = 1 To 2
    
    r.AddNew
    r!ST = Now()
    r!OBJ_RUN = strObjRun & Trim(i)
    r.Update
    
    Next i
    End Function
    Look at your code. Since you aren't specifically declaring "strObjRun" as a string variable, the first time you use it, vba is setting it to a new variant variable with a null value assigned by default. Trying to concatenate a Null variant object and a string doesn't work. Try the following:

    Code:
    Public Function fcnRunRptTimed()
    Dim d As DAO.Database
    Dim r As Recordset
    Dim strObjTbl As String
    Dim strSQL As String
    Dim i As Integer
     
    strTbl = "tbl_RptTracker"
    strSQL = "select * from " & strTbl
    Set d = CurrentDb
    Set r = d.OpenRecordset(strSQL, dbOpenDynaset)
     
    For i = 1 To 2
        r.AddNew
        r!ST = Now()
        r!OBJ_RUN = CStr(i) & getOrdinalString(i) & "Obj"
        r.Update
    Next i
     
    End Function
     
    Public Function getOrdinalString(intInput As Integer) As String
     
    Select Case Mid(CStr(intInput), Len(intInput), 1)
    'Evaluates the last digit of the input value
     
    Case "1"
    getOrdinalString = "st"
    Case "2"
    getOrdinalString = "nd"
    Case "3"
    getOrdinalString = "rd"
    Case Else
    getOrdinalString = "th"
    End Select
     
    End Function

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

Similar Threads

  1. pass value from one form to another
    By ManC in forum Forms
    Replies: 7
    Last Post: 11-25-2011, 09:59 AM
  2. Pass a value from a query to a form
    By cwwaicw311 in forum Forms
    Replies: 22
    Last Post: 03-22-2010, 10:21 AM
  3. how to pass pk to subform
    By ahmed.gomaa in forum Forms
    Replies: 9
    Last Post: 03-21-2010, 10:03 AM
  4. Pass a value from a form to another
    By cwwaicw311 in forum Forms
    Replies: 3
    Last Post: 03-16-2010, 12:42 AM
  5. pass a variable to the MsgBox function
    By 3dmgirl in forum Programming
    Replies: 2
    Last Post: 04-19-2007, 07:14 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