Function fSetDefault(strDfault As String, strCurVal As String)
'From: http://support.microsoft.com/default.aspx?scid=kb;EN-US;202117
'This function sets the default value (a string) in the table "tblDefault" it requires three
'pieces of information to operate, the current windows username which it finds With the code below.
'the name of the default which is passed through the variable "strDfault" and the current
'value of that default,which is passed through the variable "strCurVal" the value of
'the default is saved in the table "tblDfault"
'Occasionally the table does not have a default entry listed. this code does not react,
'it does nothing, it does not throw an error or anything!
'Hence the need for the function "fInitaliseDfault" below.
'This function initializes the table, adding an entry if none exists, and throwing an error
'if creating a duplicate is attempted. This error is trapped and passed back to
'the function "fInitaliseDfault" as a boolean.
On Error GoTo Err_ErrorHandler
If fInitaliseDfault(strDfault, strCurVal) Then Exit Function 'Exit if creating a duplicate is attempted
Dim strUserName As String
'
'strUserName = fGetWinUserName() 'Your windows login name
strUserName = fUserGet
strUserName = "'" & strUserName & "'"
Dim strDfaultText As String
strDfaultText = "'" & strDfault & "'"
Dim strCurText As String
strCurText = "'" & strCurVal & "'"
Dim adoCon As ADODB.Connection
Dim adoCmd As ADODB.Command
Set adoCon = CurrentProject.Connection
Set adoCmd = New ADODB.Command
Dim strSQL As String
Dim strSQL1 As String
Dim strSQL2 As String
Dim strSQL3 As String
Dim strSQL4 As String
Dim strSQL5 As String
strSQL1 = "UPDATE tblDefaults SET tblDefaults.DfaultVal = "
strSQL2 = "WHERE (((tblDefaults.DfaultUser)="
strSQL3 = ") AND ((tblDefaults.DfaultFor)="
strSQL4 = "));"
strSQL = strSQL1 & strCurText & strSQL2 & strUserName & strSQL3 & strDfaultText & strSQL4
With adoCmd
.ActiveConnection = adoCon
.CommandType = adCmdText
.CommandText = strSQL
.Execute
End With
Exit_ErrorHandler:
adoCon.Close
Set adoCon = Nothing
Set adoCmd = Nothing
Exit Function 'Sub Property
Err_ErrorHandler:
Select Case Err.Number
Case 1 'Not sure if there is an error code (1) I have never seen it yet
MsgBox "produced by error code (1) please check your code ! Error Number >>> " _
& Err.Number & " Error Desc >> " & Err.Description, , conAppName
Case Else
MsgBox "Error From --- fGetDefault --- Error Number >>> " & Err.Number _
& " <<< Error Description >> " & Err.Description, , conAppName
End Select
Resume Exit_ErrorHandler
End Function 'fSetDefault()
Function fInitaliseDfault(strDfault As String, strCurVal As String) As Boolean
'This function was necessary because the two functions above do not throw an error if the table does not
'have an entry for the particular default! This function is designed to throw an error, it attempts
'to add a new record, if it is successful the new record is added and it returns "true ".
'If it is unsuccessful an error code -2147467259 is generated which is used to generate the boolean "false "
On Error GoTo Err_ErrorHandler
Dim strUserName As String
'strUserName = fGetWinUserName() 'Your windows login name
strUserName = fUserGet
strUserName = "'" & strUserName & "'"
Dim strDfaultText As String
strDfaultText = "'" & strDfault & "'"
Dim strCurText As String
strCurText = "'" & strCurVal & "'"
Dim strSQL As String
Dim strSQL1 As String
Dim strSQL2 As String
Dim strSQL3 As String
strSQL1 = "INSERT INTO tblDefaults(DfaultUser, DfaultFor, DfaultVal) VALUES ("
strSQL2 = ", "
strSQL3 = " )"
strSQL = strSQL1 & strUserName & strSQL2 & strDfaultText & strSQL2 & strCurText & strSQL3
Dim adoCon As ADODB.Connection
Dim adoCmd As ADODB.Command
Set adoCon = CurrentProject.Connection
Set adoCmd = New ADODB.Command
'Use a Command Object to issue an SQL statement
With adoCmd
.ActiveConnection = adoCon
.CommandType = adCmdText
.CommandText = strSQL
.Execute
End With
fInitaliseDfault = True
Exit_ErrorHandler:
adoCon.Close
Set adoCon = Nothing
Set adoCmd = Nothing
Exit Function
Err_ErrorHandler:
Select Case Err.Number
Case -2147467259 'An expected error --- duplicate values
'MsgBox "An expected error --- duplicate values", , "Error Number >>> " & Err.Number & _
" Error Desc >> " & Err.Description, , conAppName
fInitaliseDfault = False 'Default Already initialized
Case Else
MsgBox "Error From --- fInitaliseDfault() --- Error Number >>> " _
& Err.Number & " <<< Error Description >> " & Err.Description, , conAppName
End Select
Resume Exit_ErrorHandler
End Function ' fInitaliseDfault()