0

I thought this would be easy but the following code gives...

Microsoft VBScript runtime error '800a01a8'

Object required: 'Application(...)'

/include/setup.asp, line 7

Function getConnectionString

    Dim connectionString

    Set connectionString = Application("ConnString")

    If connectionString Is Nothing Then

        Set connectionString = loadConnectionString

        Application.Lock
        Application("ConnString") = connectionString
        Application.Unlock

    End If

    getConnectionString = connectionString

End Function

loadConnectionString is another function.

How do I fix this?

user692942
  • 16,398
  • 7
  • 76
  • 175
Ian Warburton
  • 15,170
  • 23
  • 107
  • 189
  • Possible duplicate of [Check if an Object exists in VBScript](http://stackoverflow.com/questions/4100506/check-if-an-object-exists-in-vbscript) – Dijkgraaf Dec 07 '15 at 21:41
  • 1
    This one... Set connectionString = Application("ConnString") – Ian Warburton Dec 07 '15 at 21:43
  • 1
    Issue is `Application("ConnString")` contains a connection `string` not an `object` so `Set` should not be used. Only use `Set` to store object instances in variables. – user692942 Dec 07 '15 at 21:54

1 Answers1

1

The problem here is connectionString variable is being treated as an Object when it in fact contains a string.

Set should only be used to store Object variables, this goes for Application("varname") style variables as well. Remove the Is Nothing check as this also refers to Object variables and replace it with a Len(connectionString) < 1 check.

Function getConnectionString() 
    Dim connectionString
    connectionString = Application("ConnString")

    If Len(connectionString & "") < 1 Then
        'loadConnectionString() should return a string not an Object
        'may need to be amended.
        connectionString = loadConnectionString()
        Application.Lock
        Application("ConnString") = connectionString
        Application.Unlock
    End If
    getConnectionString = connectionString
End Function
user692942
  • 16,398
  • 7
  • 76
  • 175