Invalid use of Null when explicitly assigning Null to a variable of type variantNull values for variables in VBA“invalid use of null” for vba function that returns variantInvalid use of null Access-VBAType mismatch when comparing two variants, why?VBA SQL doesn't recognize assigned variablesInvalid Use of Null in Dropdown VBAAssigning a string to a variable in Access VBAReturning editable ADO Recordsets to an MS Access Form using a Class ModuleInvalid field Data type when use TableDef.CreateField Method VBAMS access “Invalid use of Null”

How bad would a partial hash leak be, realistically?

Pronoun introduced before its antecedent

Diet Coke or water?

Old black and white movie: glowing black rocks slowly turn you into stone upon touch

Traffic law UK, pedestrians

What flavor of zksnark in tezos

How could a possessed body begin to rot and decay while it is still alive?

Chopin: marche funèbre bar 15 impossible place

Shrink exponential fraction argument

What is the purpose of building foundations?

Accidentally renamed tar.gz file to a non tar.gz file, will my file be messed up

Did Darth Vader wear the same suit for 20+ years?

Riley's, assemble!

X-shaped crossword

Pros and cons of writing a book review?

Can Green-Flame Blade be cast twice with the Hunter ranger's Horde Breaker ability?

Is it legal in the UK for politicians to lie to the public for political gain?

How certain is a caster of when their spell will end?

The ring of global sections of a regular scheme

Is there any word or phrase for negative bearing?

Using new lumber in an old wall with larger lumber dimensions

Is it a problem that pull requests are approved without any comments

Company is asking me to work from overseas, but wants me to take a paycut

Do I include animal companions when calculating difficulty of an encounter?



Invalid use of Null when explicitly assigning Null to a variable of type variant


Null values for variables in VBA“invalid use of null” for vba function that returns variantInvalid use of null Access-VBAType mismatch when comparing two variants, why?VBA SQL doesn't recognize assigned variablesInvalid Use of Null in Dropdown VBAAssigning a string to a variable in Access VBAReturning editable ADO Recordsets to an MS Access Form using a Class ModuleInvalid field Data type when use TableDef.CreateField Method VBAMS access “Invalid use of Null”






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am currently trying to upgrade an old ADP project from Access 2010 x64 to Access 2019 x64. I have managed to convert it to an .accdb file, but are now running into errors with my VBA code.



Please consider the following function:



Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
Dim cnTemp As ADODB.Connection, rsTemp As ADODB.Recordset
Dim sSQL As String
On Error GoTo LAB_Error
sSQL = "SELECT T_Value FROM INT_SystemSettings WHERE (T_Key = '" & sKey & "')"
Set cnTemp = New ADODB.Connection
Set rsTemp = New ADODB.Recordset
cnTemp.CursorLocation = adUseServer
cnTemp.Open CurrentProject.BaseConnectionString
rsTemp.Open sSQL, cnTemp, adOpenForwardOnly, adLockReadOnly
If (rsTemp.EOF) Then GoTo LAB_Error
vValue = Nz(rsTemp![T_Value])
rsTemp.Close
cnTemp.Close
On Error GoTo 0
GetSystemSetting = True
Exit Function
LAB_Error:
vValue = Null
If (rsTemp.State <> adStateClosed) Then rsTemp.Close
If (cnTemp.State <> adStateClosed) Then cnTemp.Close
GetSystemSetting = False
End Function


I know that this code is questionable in many aspects, but would like to focus on the line



vValue = Null


When this line gets executed, a runtime error is raised:



Invalid use of Null


I have read dozens of articles about that error message on various sites, including this one, but it always boiled down to that the OP hadn't made the destination variable a variant. But in my case, the destination variable, vValue, is of type variant. Furthermore, that code ran since 8 years without any problem in Access 2010 x64.



What is the reason for that error, and how can I prevent it?










share|improve this question

















  • 2





    How are you calling this function? vValue isn't a local variable, it is (ByRef) the variable from the calling function, which is not necessarily a Variant.

    – Andre
    Mar 24 at 13:22






  • 1





    And another thing, you could replace this whole function with a simple DLookup call.

    – Andre
    Mar 24 at 13:24











  • @Andre Thank you very much! That was the problem ... not sure how I could miss it (my last VBA coding is several years back ...). Regarding DLookup, I'm not sure if it does the locking exactly as the original author of the function intended; in general, I'd like to change as less as possible in this beast (LOTS of code in there ...). Would you mind making your comments an answer?

    – Binarus
    Mar 24 at 14:16


















1















I am currently trying to upgrade an old ADP project from Access 2010 x64 to Access 2019 x64. I have managed to convert it to an .accdb file, but are now running into errors with my VBA code.



Please consider the following function:



Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
Dim cnTemp As ADODB.Connection, rsTemp As ADODB.Recordset
Dim sSQL As String
On Error GoTo LAB_Error
sSQL = "SELECT T_Value FROM INT_SystemSettings WHERE (T_Key = '" & sKey & "')"
Set cnTemp = New ADODB.Connection
Set rsTemp = New ADODB.Recordset
cnTemp.CursorLocation = adUseServer
cnTemp.Open CurrentProject.BaseConnectionString
rsTemp.Open sSQL, cnTemp, adOpenForwardOnly, adLockReadOnly
If (rsTemp.EOF) Then GoTo LAB_Error
vValue = Nz(rsTemp![T_Value])
rsTemp.Close
cnTemp.Close
On Error GoTo 0
GetSystemSetting = True
Exit Function
LAB_Error:
vValue = Null
If (rsTemp.State <> adStateClosed) Then rsTemp.Close
If (cnTemp.State <> adStateClosed) Then cnTemp.Close
GetSystemSetting = False
End Function


I know that this code is questionable in many aspects, but would like to focus on the line



vValue = Null


When this line gets executed, a runtime error is raised:



Invalid use of Null


I have read dozens of articles about that error message on various sites, including this one, but it always boiled down to that the OP hadn't made the destination variable a variant. But in my case, the destination variable, vValue, is of type variant. Furthermore, that code ran since 8 years without any problem in Access 2010 x64.



What is the reason for that error, and how can I prevent it?










share|improve this question

















  • 2





    How are you calling this function? vValue isn't a local variable, it is (ByRef) the variable from the calling function, which is not necessarily a Variant.

    – Andre
    Mar 24 at 13:22






  • 1





    And another thing, you could replace this whole function with a simple DLookup call.

    – Andre
    Mar 24 at 13:24











  • @Andre Thank you very much! That was the problem ... not sure how I could miss it (my last VBA coding is several years back ...). Regarding DLookup, I'm not sure if it does the locking exactly as the original author of the function intended; in general, I'd like to change as less as possible in this beast (LOTS of code in there ...). Would you mind making your comments an answer?

    – Binarus
    Mar 24 at 14:16














1












1








1








I am currently trying to upgrade an old ADP project from Access 2010 x64 to Access 2019 x64. I have managed to convert it to an .accdb file, but are now running into errors with my VBA code.



Please consider the following function:



Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
Dim cnTemp As ADODB.Connection, rsTemp As ADODB.Recordset
Dim sSQL As String
On Error GoTo LAB_Error
sSQL = "SELECT T_Value FROM INT_SystemSettings WHERE (T_Key = '" & sKey & "')"
Set cnTemp = New ADODB.Connection
Set rsTemp = New ADODB.Recordset
cnTemp.CursorLocation = adUseServer
cnTemp.Open CurrentProject.BaseConnectionString
rsTemp.Open sSQL, cnTemp, adOpenForwardOnly, adLockReadOnly
If (rsTemp.EOF) Then GoTo LAB_Error
vValue = Nz(rsTemp![T_Value])
rsTemp.Close
cnTemp.Close
On Error GoTo 0
GetSystemSetting = True
Exit Function
LAB_Error:
vValue = Null
If (rsTemp.State <> adStateClosed) Then rsTemp.Close
If (cnTemp.State <> adStateClosed) Then cnTemp.Close
GetSystemSetting = False
End Function


I know that this code is questionable in many aspects, but would like to focus on the line



vValue = Null


When this line gets executed, a runtime error is raised:



Invalid use of Null


I have read dozens of articles about that error message on various sites, including this one, but it always boiled down to that the OP hadn't made the destination variable a variant. But in my case, the destination variable, vValue, is of type variant. Furthermore, that code ran since 8 years without any problem in Access 2010 x64.



What is the reason for that error, and how can I prevent it?










share|improve this question














I am currently trying to upgrade an old ADP project from Access 2010 x64 to Access 2019 x64. I have managed to convert it to an .accdb file, but are now running into errors with my VBA code.



Please consider the following function:



Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
Dim cnTemp As ADODB.Connection, rsTemp As ADODB.Recordset
Dim sSQL As String
On Error GoTo LAB_Error
sSQL = "SELECT T_Value FROM INT_SystemSettings WHERE (T_Key = '" & sKey & "')"
Set cnTemp = New ADODB.Connection
Set rsTemp = New ADODB.Recordset
cnTemp.CursorLocation = adUseServer
cnTemp.Open CurrentProject.BaseConnectionString
rsTemp.Open sSQL, cnTemp, adOpenForwardOnly, adLockReadOnly
If (rsTemp.EOF) Then GoTo LAB_Error
vValue = Nz(rsTemp![T_Value])
rsTemp.Close
cnTemp.Close
On Error GoTo 0
GetSystemSetting = True
Exit Function
LAB_Error:
vValue = Null
If (rsTemp.State <> adStateClosed) Then rsTemp.Close
If (cnTemp.State <> adStateClosed) Then cnTemp.Close
GetSystemSetting = False
End Function


I know that this code is questionable in many aspects, but would like to focus on the line



vValue = Null


When this line gets executed, a runtime error is raised:



Invalid use of Null


I have read dozens of articles about that error message on various sites, including this one, but it always boiled down to that the OP hadn't made the destination variable a variant. But in my case, the destination variable, vValue, is of type variant. Furthermore, that code ran since 8 years without any problem in Access 2010 x64.



What is the reason for that error, and how can I prevent it?







vba ms-access access-vba






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 24 at 13:09









BinarusBinarus

1,79611029




1,79611029







  • 2





    How are you calling this function? vValue isn't a local variable, it is (ByRef) the variable from the calling function, which is not necessarily a Variant.

    – Andre
    Mar 24 at 13:22






  • 1





    And another thing, you could replace this whole function with a simple DLookup call.

    – Andre
    Mar 24 at 13:24











  • @Andre Thank you very much! That was the problem ... not sure how I could miss it (my last VBA coding is several years back ...). Regarding DLookup, I'm not sure if it does the locking exactly as the original author of the function intended; in general, I'd like to change as less as possible in this beast (LOTS of code in there ...). Would you mind making your comments an answer?

    – Binarus
    Mar 24 at 14:16













  • 2





    How are you calling this function? vValue isn't a local variable, it is (ByRef) the variable from the calling function, which is not necessarily a Variant.

    – Andre
    Mar 24 at 13:22






  • 1





    And another thing, you could replace this whole function with a simple DLookup call.

    – Andre
    Mar 24 at 13:24











  • @Andre Thank you very much! That was the problem ... not sure how I could miss it (my last VBA coding is several years back ...). Regarding DLookup, I'm not sure if it does the locking exactly as the original author of the function intended; in general, I'd like to change as less as possible in this beast (LOTS of code in there ...). Would you mind making your comments an answer?

    – Binarus
    Mar 24 at 14:16








2




2





How are you calling this function? vValue isn't a local variable, it is (ByRef) the variable from the calling function, which is not necessarily a Variant.

– Andre
Mar 24 at 13:22





How are you calling this function? vValue isn't a local variable, it is (ByRef) the variable from the calling function, which is not necessarily a Variant.

– Andre
Mar 24 at 13:22




1




1





And another thing, you could replace this whole function with a simple DLookup call.

– Andre
Mar 24 at 13:24





And another thing, you could replace this whole function with a simple DLookup call.

– Andre
Mar 24 at 13:24













@Andre Thank you very much! That was the problem ... not sure how I could miss it (my last VBA coding is several years back ...). Regarding DLookup, I'm not sure if it does the locking exactly as the original author of the function intended; in general, I'd like to change as less as possible in this beast (LOTS of code in there ...). Would you mind making your comments an answer?

– Binarus
Mar 24 at 14:16






@Andre Thank you very much! That was the problem ... not sure how I could miss it (my last VBA coding is several years back ...). Regarding DLookup, I'm not sure if it does the locking exactly as the original author of the function intended; in general, I'd like to change as less as possible in this beast (LOTS of code in there ...). Would you mind making your comments an answer?

– Binarus
Mar 24 at 14:16













1 Answer
1






active

oldest

votes


















1














It is important to remember that with functions like this:



Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
vValue = Null


unless you specify ByVal, the parameters are passed ByRef, and so you are actually writing to the variable that is used as parameter when calling the function.



If that variable isn't a variant, the error is triggered.



Dim str As String
If GetSystemSetting("non-existing", str) Then ' KA-BOOM!



An alternative with DLookup would be the following. It should behave exactly the same, unless you have valid SystemSettings that are NULL.



Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
' DLookup returns NULL if no record is found
vValue = DLookup("T_Value", "INT_SystemSettings", "T_Key = '" & sKey & "'")
GetSystemSetting = Not IsNull(vValue)
End Function


DLookup is a read-only operation, so it should be the same regarding locking.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324125%2finvalid-use-of-null-when-explicitly-assigning-null-to-a-variable-of-type-variant%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    It is important to remember that with functions like this:



    Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
    vValue = Null


    unless you specify ByVal, the parameters are passed ByRef, and so you are actually writing to the variable that is used as parameter when calling the function.



    If that variable isn't a variant, the error is triggered.



    Dim str As String
    If GetSystemSetting("non-existing", str) Then ' KA-BOOM!



    An alternative with DLookup would be the following. It should behave exactly the same, unless you have valid SystemSettings that are NULL.



    Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
    ' DLookup returns NULL if no record is found
    vValue = DLookup("T_Value", "INT_SystemSettings", "T_Key = '" & sKey & "'")
    GetSystemSetting = Not IsNull(vValue)
    End Function


    DLookup is a read-only operation, so it should be the same regarding locking.






    share|improve this answer



























      1














      It is important to remember that with functions like this:



      Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
      vValue = Null


      unless you specify ByVal, the parameters are passed ByRef, and so you are actually writing to the variable that is used as parameter when calling the function.



      If that variable isn't a variant, the error is triggered.



      Dim str As String
      If GetSystemSetting("non-existing", str) Then ' KA-BOOM!



      An alternative with DLookup would be the following. It should behave exactly the same, unless you have valid SystemSettings that are NULL.



      Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
      ' DLookup returns NULL if no record is found
      vValue = DLookup("T_Value", "INT_SystemSettings", "T_Key = '" & sKey & "'")
      GetSystemSetting = Not IsNull(vValue)
      End Function


      DLookup is a read-only operation, so it should be the same regarding locking.






      share|improve this answer

























        1












        1








        1







        It is important to remember that with functions like this:



        Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
        vValue = Null


        unless you specify ByVal, the parameters are passed ByRef, and so you are actually writing to the variable that is used as parameter when calling the function.



        If that variable isn't a variant, the error is triggered.



        Dim str As String
        If GetSystemSetting("non-existing", str) Then ' KA-BOOM!



        An alternative with DLookup would be the following. It should behave exactly the same, unless you have valid SystemSettings that are NULL.



        Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
        ' DLookup returns NULL if no record is found
        vValue = DLookup("T_Value", "INT_SystemSettings", "T_Key = '" & sKey & "'")
        GetSystemSetting = Not IsNull(vValue)
        End Function


        DLookup is a read-only operation, so it should be the same regarding locking.






        share|improve this answer













        It is important to remember that with functions like this:



        Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
        vValue = Null


        unless you specify ByVal, the parameters are passed ByRef, and so you are actually writing to the variable that is used as parameter when calling the function.



        If that variable isn't a variant, the error is triggered.



        Dim str As String
        If GetSystemSetting("non-existing", str) Then ' KA-BOOM!



        An alternative with DLookup would be the following. It should behave exactly the same, unless you have valid SystemSettings that are NULL.



        Public Function GetSystemSetting(sKey As String, vValue As Variant) As Boolean
        ' DLookup returns NULL if no record is found
        vValue = DLookup("T_Value", "INT_SystemSettings", "T_Key = '" & sKey & "'")
        GetSystemSetting = Not IsNull(vValue)
        End Function


        DLookup is a read-only operation, so it should be the same regarding locking.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 24 at 19:09









        AndreAndre

        20.6k42152




        20.6k42152



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324125%2finvalid-use-of-null-when-explicitly-assigning-null-to-a-variable-of-type-variant%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            위키백과:대문 둘러보기 메뉴기부 안내모바일판 대문크리에이티브 커먼즈 저작자표시-동일조건변경허락 3.0CebuanoDeutschEnglishEspañolFrançaisItaliano日本語NederlandsPolskiPortuguêsРусскийSvenskaTiếng ViệtWinaray中文العربيةCatalàفارسیSrpskiУкраїнськаБългарскиНохчийнČeštinaDanskEsperantoEuskaraSuomiעבריתMagyarՀայերենBahasa IndonesiaҚазақшаBaso MinangkabauBahasa MelayuBân-lâm-gúNorskRomânăSrpskohrvatskiSlovenčinaTürkçe

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh