View Full Version : To check whether the property in access exits using vb


asmita
07-05-2005, 11:57 PM
hi

i am creating a property for the startupform and then adding it to the application. the code for the same is given below:

Set prp = db.CreateProperty("StartupForm", dbText, "SwitchBoard", False)
db.Properties.Append prp
db.Properties("StartupForm") = "SwitchBoard"

the above code creates a property and then adds it to the application.

but if the property already exist then it gives error saying property of the same name already exist

before appending the property i want to check whether the property exists if it exits then i just need to change the name of the form to "switchboard"

can some1 pls help.

thanks in advance :)

john471
07-06-2005, 12:09 AM
From memory the Access help pages suggest using the reverse tactic... attempt to change the porperty value (assume it exists) and if that errors with a specific error number, it doesn't exist so create & append it.



..... '(inside your current sub/function)
bResult = bChangeProperty "StartupForm", dbText, "SwitchBoard"
..... 'requires bChangeProperty function, below

Private Function bChangeProperty(szPropName As String, varPropType As Variant, varPropValue As Variant) As Boolean
Dim dbs As Database, prp As Property
Const conPropNotFoundError = 3270

Set dbs = CurrentDb
On Error GoTo Change_Err
dbs.Properties(szPropName) = varPropValue
bChangeProperty = True

Change_Exit:
Exit Function

Change_Err:
If Err = conPropNotFoundError Then ' Property not found.
Set prp = dbs.CreateProperty(szPropName, varPropType, varPropValue)
dbs.Properties.Append prp
Resume Next
Else
' Unknown error.
bChangeProperty = False
Resume Change_Exit
End If
End Function


Hope this helps

Regards

John

asmita
07-06-2005, 12:38 AM
i executed the code given by you. but if there a form set as a startupform ans then if i execute the code then it gives me type mismatch error on the line marked in red can u pls help


Change_Err:
If Err = conPropNotFoundError Then ' Property not found.
Set prp = dbs.CreateProperty(szPropName, varPropType, varPropValue)
dbs.Properties.Append prp
Resume Next
Else
' Unknown error.
bChangeProperty = False
Resume Change_Exit
End If

john471
07-06-2005, 12:43 AM
ooops.

Try changing the variable declaration line to:-

Dim dbs As DAO.Database, prp As DAO.Property
You'll need to set a reference to DAO, if you have not already.

Let me know how you go.