Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    nd0911 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    31

    Small query function don't work on runtime version

    Hello,

    After struggling with this issue I found that the following function returns an error (only on Access runtime version).

    I will be happy to hear what I'm doing wrong.



    Thank you.

    Code:
    Public Sub SetId_9(tblName As String, IdField As String) 
    Dim SQL As String
    
    
    
    
    SQL = "UPDATE " & tblName _
       & " SET [" & IdField & "] = String(9 - Len([" & IdField & "])," & """" & "0" & """)" & " & [" & IdField & "]"
    
    
    
    
    CurrentDb.Execute SQL
    
    
    End Sub

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    we would be happy to hear what the error is, might provide a clue as to the problem. Also some example data, you appear to be updating a field named idField with a variation of that name. Also not sure what the string function is.

  3. #3
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,405
    Purpose seems to be to add leading zeros to the existing IdField to make its length equal 9 characters.
    Don't know if this will fix the problem, but might make it easier to debug.
    Also, the STRING function was introduced in AC2013. If your runtime version predates that it would fail.
    In that case, you could try some alternate code to add the leading zeros without the STRING function.

    Code:
    Public Sub SetId_9(tblName As String, IdField As String)
        Dim SQL As String
        Dim arg As String
        if len(IdField) > 9 then
            msgbox "Length > 9"
            exit sub
        end if
        arg = String(9 - Len(IdField), "0") & IdField 
    
        Debug.Print arg
        SQL = "UPDATE " & tblName & " SET " & IdField = arg
     
        Debug.Print SQL
    
    
        Currentdb.Execute SQL
    End Sub

  4. #4
    jwhite is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2012
    Location
    North Carolina
    Posts
    349
    The String function is in Ac2000.

  5. #5
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    Also, the STRING function was introduced in AC2013
    learn something new - but think it may be earlier as it works for me in 2010

    but if I type

    ?String(9 - Len([" & "ABC" & "])," & """" & "0" & """)" & " & [" & "ABC" & "]"

    in the immediate window I get a compile error - Expected: )

    So I don't think the code has ever worked, even in a full version of access

    and if idField is numeric, you cannot add preceding zeros which would make it a string.

  6. #6
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    is the id an autonumber field?

    I changed a similar field to a text field, took out the key and it works perfectly

  7. #7
    nd0911 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    31
    Hi, first, thank you,

    Second, my IdField is a string field but contains only and always numbers, my goal is if the number in the cell is less the 9 characters then add zero's at the beginning.

    any suggestions ?

  8. #8
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    I'm assuming it's not a key field? Is it linked to another field?

  9. #9
    nd0911 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    31
    You right it is not a key field and also not link to another field, actually I do not have any relationships of any kind, just tables.

  10. #10
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    I'm assuming it's not a key field? Is it linked to another field?

  11. #11
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Is the table open when you run the function or a form which has it as a data source.

  12. #12
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    perhaps time to review the code you are actually using now - and description of any errors you may be getting. You haven't said whether you have tried davegri's suggestion

  13. #13
    nd0911 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    31
    The problem is that I'm new with Access but I'm very good with VBA (on Excel) and my problems is with my PC at work, and now I'm home, here I have full Office version with Access and over there I do not have Access, and I want to simulate the Runtime invairment like there and I dont know if it is possible.

  14. #14
    nd0911 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    31
    Ok I take an old Laptop and intall runtime.
    Now for sure the String function is what causing the error, I removed the String function and it worked.

    Is there some alternative query for my needs ?

    Thank you.

  15. #15
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,405
    If you want to simulate the runtime (without installing it) on a PC that has full Access installed, change the suffix to .accdr and run it.
    As for an alternate method:
    Code:
    Public Sub SetId_9(tblName As String, IdField As String)
        Dim SQL As String
        Dim arg As String
        if len(IdField) > 9 then
            msgbox "Length > 9"
            exit sub
        end if
        arg = format(IdField,"000000000") 
    
        Debug.Print arg
        SQL = "UPDATE " & tblName & " SET IdField = " & arg
    
        Debug.Print SQL
    
        Currentdb.Execute SQL
    End Sub

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 01-18-2017, 09:11 AM
  2. ms access runtime version for other computers
    By charlesgardner51 in forum Access
    Replies: 1
    Last Post: 09-09-2015, 03:27 PM
  3. Runtime version Access 2003 error 429
    By Docjsj in forum Programming
    Replies: 1
    Last Post: 06-25-2015, 08:14 AM
  4. 2010 Runtime version
    By crowegreg in forum Access
    Replies: 4
    Last Post: 08-10-2012, 11:48 AM
  5. Replies: 1
    Last Post: 02-10-2012, 05:53 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