Why does Recordset.Update not always work? - vba

I'm working on a database in MS Access. Since it has a fair amount of VBA code, I've neatly split up the code into different modules, and implemented an error handler which writes the error into a table into the database. I've never had any problems with it, until there was an error in the login system (runtime error 49: "invalid use of null" or something similar - my system is set up in German). In that case, for some reason, the error doesn't get logged. I've been able to reproduce this on two machines. I've fixed the error in the meantime, but I want to know why it didn't log the error properly, because I want the error reporting to work properly.
Here's the code in my error handler:
Public Function RuntimeError(Location As String, ErrNumber As String, ErrSource As String, ErrDescription As String, ErrHelpFile As String, ErrHelpContext As String, ErrLastDLLError As String)
On Error GoTo Fehler
Dim LVDB As DAO.Database
Set LVDB = CurrentDb
Dim LUKSVDB_Fehler As DAO.Recordset
Set LUKSVDB_Fehler = LVDB.OpenRecordset("LUKSVDB_Fehler")
LUKSVDB_Fehler.AddNew
LUKSVDB_Fehler!Location = Location
LUKSVDB_Fehler!ErrNumber = ErrNumber
LUKSVDB_Fehler!ErrSource = ErrSource
LUKSVDB_Fehler!ErrDescription = ErrDescription
LUKSVDB_Fehler!ErrHelpFile = ErrHelpFile
LUKSVDB_Fehler!ErrHelpContext = ErrHelpContext
LUKSVDB_Fehler!ErrLastDLLError = ErrLastDLLError
LUKSVDB_Fehler!Person = User
LUKSVDB_Fehler!Zeit = Now
'Ask the user if they would like to add any aditional information, like what they did to cause the error
If ErrDescription <> "Folgefehler" Then LUKSVDB_Fehler!Nutzererklärung = InputBox("Es ist ein Fehler aufgetreten. Kannst du noch ein paar Angaben machen, was du gemacht hast, um die Fehlerbehebung zu erleichtern? Details für den Entwickler: " & Location & " " & ErrNumber & " " & ErrSource & " " & ErrDescription & " " & ErrHelpFile & " " & ErrHelpContext & " " & ErrLastDLLError & " " & User, "Systemfehler")
LUKSVDB_Fehler.Update
'error handling logic
Done:
Exit Function
Fehler:
MsgBox "Die Fehlermeldung konnte nicht gespeichert werden. Bitte diese Informationen dem Entwickler mitteilen: " & Location & " " & ErrNumber & " " & ErrSource & " " & ErrDescription & " " & ErrHelpFile & " " & ErrHelpContext & " " & ErrLastDLLError & " " & User & ". Fehler beim Speichern: " & Err.Number & " " & Err.Source & " " & Err.Description & " " & Err.HelpFile & " " & Err.HelpContext & " " & Err.LastDllError, vbCritical
DoCmd.Quit
End Function
I've checked to make sure that none of the fields in the table are required (except for the primary key, which is an autonumber field anyway). The weird thing is, usually, it works, but in some constellations obviously not. It also doesn't throw an error. Here are the values the function RuntimeError gets that somehow makes it not work:
: Location : "Form_Willkommen: Form_Open" : String
: ErrNumber : "94" : String
: ErrSource : "LUKS-Verwaltung" : String
: ErrDescription : "Unzulässige Verwendung von Null" : String
: ErrHelpFile : "C:\Program Files (x86)\Common Files\Microsoft Shared\VBA\VBA7.1\1031\VbLR6.chm" : String
: ErrHelpContext : "1000094" : String
: ErrLastDLLError : "0" : String
If I stop the code on the LUKSVDB_Fehler.Update line and look into the local values, everything seems to be fine. All the values have been added to the record, it also has a value for the primary key, but as soon as it hits Update, it just seems to throw it all out of the window, but it also doesn't generate an error, either.
I've also tried the force option on the .Update method in the hopes that it throws an error so I can see exactly what's not working, but that isn't very well documented, and I can't seem to get the syntax right (I tried .Update , True).
Does anyone have any idea what I could be doing wrong, or how I could get it to give me a proper error when .Update doesn't work?
In case it matters, I'm using Access on Office 365.

Related

Run-Time Error '13' Type Mismatch - ACCESS DATABASE

I am trying to compare two text fields txtTrailerNumber and txtSealNumber to the database table Tab_TrailerDetails. [TrailerNumber] and [SealNumber] as listed in the table.
I am trying to get the database to look at the trailer number entered into the form, and if it finds a duplicate value it then looks at the seal number entered into the form. If both values have a duplicate found in the table it should throw up the Msg_Box error code.
Private Sub txtSealNumber_AfterUpdate()
Dim NewTrailer, NewSeal As String
Dim stLinkCriteria As String
'Assign the entered Trailer Number and Seal Number to a variable
NewTrailer = Me.txtTrailerNumber.Value
NewSeal = Me.txtSealNumber.Value
stLinkCriteria = ("[TrailerNumber]='" & NewTrailer & "'" And "[SealNumber]='" & NewSeal & "'")
If Me.txtTrailerNumber = DLookup("[TrailerNumber]", "Tab_TrailerDetails", stLinkCriteria) Then
MsgBox "This trailer, " & NewTrailer & ", has already been entered in database," _
& vbCr & vbCr & "along with seal " & NewSeal & "" _
& vbCr & vbCr & "Please make sure Trailer and Seal are not already entered.", vbInformation, "Duplicate information"
'undo the process and clear all fields
Me.Undo
End If
End Sub
The cause of the error is that you have a logical keyword, notably AND inside a string expression. Change your code to
stLinkCriteria = ("[TrailerNumber]='" & NewTrailer & "' And [SealNumber]='" & NewSeal & "'")

WUApiLib.iupdate VB.net retrieving properties error

I have the following code to check windows updates
Function CheckWinUpdates() As Integer
CheckWinUpdates = 0
Dim WUSession As UpdateSession
Dim WUSearcher As UpdateSearcher
Dim WUSearchResults As ISearchResult
Try
WUSession = New UpdateSession
WUSearcher = WUSession.CreateUpdateSearcher()
WUSearchResults = WUSearcher.Search("IsInstalled=0 and Type='Software'")
CheckWinUpdates = WUSearchResults.Updates.Count
Catch ex As Exception
CheckWinUpdates = -1
End Try
If CheckWinUpdates > 0 Then
Try
'Dim Update As IUpdate
Dim i As Integer = 0
For i = 0 To WUSearchResults.Updates.Count - 1
'Update = WUSearchResults.Updates.Item(i)
EventLog.WriteEntry("Item is type: " & WUSearchResults.Updates.Item(i).ToString, EventLogEntryType.Information, 85)
EventLog.WriteEntry("Deadline: " & WUSearchResults.Updates.Item(i).Deadline.ToString, EventLogEntryType.Information, 85)
EventLog.WriteEntry("Type: " & WUSearchResults.Updates.Item(i).Type.ToString, EventLogEntryType.Information, 85)
EventLog.WriteEntry("Released on: " & WUSearchResults.Updates.Item(i).LastDeploymentChangeTime, EventLogEntryType.Information, 85)
EventLog.WriteEntry("This windows update is required: " & WUSearchResults.Updates.Item(i).Title, EventLogEntryType.Information, 85)
'EventLog.WriteEntry("This windows update is required: " & Update.Title & vbCrLf & "Released on: " &
' Update.LastDeploymentChangeTime & vbCrLf & "Type: " & Update.Type.ToString & vbCrLf &
' "Deadline: " & Update.Deadline.ToString & vbCrLf & vbCrLf & "Item is type: " & Update.ToString, EventLogEntryType.Information, 85)
Next
Catch ex As Exception
EventLog.WriteEntry("Error while attempting to log required updates:" & vbCrLf & ex.Message, EventLogEntryType.Error, 86)
End Try
End If
WUSearchResults = Nothing
WUSearcher = Nothing
WUSession = Nothing
End Function
My intention with this is to a) get the number of windows updates that are applicable, and b) to look at what other properties are available, and more specifically see how many are older than a week or 2.
I know that UpdateSearcher doesn't allow to search by date, so I am looking to iterate through each item and then later report on each one.
At the moment my function does quite happily return the number of updtes, but when I try to get any of the properties I get "Object reference not set to an instance of an object".
Any ideas where I'm going wrong?
I got this working, turns it it doesn't like the deadline property, I don't know it comes up with "object reference not set", but for what I need it doesn't matter.

How can I manage error code SQL in MS access form database?

I want manage SQL server error code in access form
sample duplicate error from SQL server
In Access VBA, you need to use:
On Error GoTo Error_Handler
' YOUR CODE HERE
.
.
.
Return_Label:
Exit Function
Error_Handler:
'What goes here depends on the data access model
Resume Return_Label
You may have to retrieve the Errors collection of the Error object as described here.
It shows this example code:
Sub DescriptionX()
Dim dbsTest As Database
On Error GoTo ErrorHandler
' Intentionally trigger an error.
Set dbsTest = OpenDatabase("NoDatabase")
Exit Sub
ErrorHandler:
Dim strError As String
Dim errLoop As Error
' Enumerate Errors collection and display properties of
' each Error object.
For Each errLoop In Errors
With errLoop
strError = _
"Error #" & .Number & vbCr
strError = strError & _
" " & .Description & vbCr
strError = strError & _
" (Source: " & .Source & ")" & vbCr
strError = strError & _
"Press F1 to see topic " & .HelpContext & vbCr
strError = strError & _
" in the file " & .HelpFile & "."
End With
MsgBox strError
Next
Resume Next
End Sub

VBA in ACCESS how "Flag" and "Err05" got declared ?

I do not understand where "Flag and Err05" (at the bottom of the codes)have been declared ? I have searched the entire project and can not found where the two parameters got declared. When I try to use the below codes in another project , i got the error message "Run time error : items not found in this collection " Any answers?
>Private Sub SanityCheck_Click()
>Dim St As String, WrnFlag As Boolean, j As Integer
>Dim RS As Recordset, RS1 As Recordset, i As Integer, WrongRootCauses As String
>Dim Err01 As String, Err02 As String, Err03 As String, Err04 As String
>WrnFlag = False
>If IsNull(Me.FactoryList) Then
> MsgBox "Select valid Factory before proceeding.", vbExclamation
>Else
> St = "SELECT Factory, Step1, Step2, Step3, Step4, DateColumn FROM [tbl >Factory] WHERE Allowed = True and Factory = """ & Me.FactoryList.Value & """"
> Set RS = CurrentDb.OpenRecordset(St, dbOpenSnapshot)
'-- 12NC codes check --
> St = "SELECT Count([" & RS!Step2 & "].PLANT) AS 12NC_chk " & _
>"FROM [" & RS!Step2 & "] " & _
> "WHERE (((Len(Trim(IIf(Left(Trim([12NC_CODE]),3)=""000"",Right>([12NC_CODE],Len([12NC_CODE])-3),[12NC_CODE])))) Not In (0,1,12)));"
' "WHERE (((Len(Trim([12NC_CODE]))) Not In (0,1,12)));"
> Set RS1 = CurrentDb.OpenRecordset(St, dbOpenSnapshot)
>If RS1.BOF And RS1.EOF Then
> Err05 = "+ No 12NC issues found."
>Else
> RS1.MoveFirst
> RS1.MoveLast
> If RS1![12NC_chk] = 0 Then
> Err05 = "+ No 12NC issues found."
> Else
> Err05 = "- " & RS1![12NC_chk] & " Incorrect 12NCs found!"
> WrnFlag = True
> End If
> End If
'--
> If WrnFlag = True Then **Flag** = vbCritical Else **Flag** = vbInformation
> MsgBox RS!Factory & " input data sanity check:" & vbCrLf & vbCrLf & " " & >Err01 & vbCrLf & " " & Err02 & vbCrLf & " " & Err03 & _
> vbCrLf & " " & Err04 & vbCrLf & " " & Err05 & vbCrLf & vbCrLf & IIf>>(WrongRootCauses = "", "", " Unknown ADP Root Cause(s):" & vbCrLf & WrongRootCauses), Flag
> RS.Close
>RS1.Close
>End If
>End Sub
Two posibilities
Unless the file have Option explicit in the top, you are not forced to explicitely declare variables. Which means that Flag and Err05 have indeed never been declared. VBA will just create them on the fly with a Variant type and an initial value of Nothing.
They might be global variables declared outside of any sub in another file. Try right clicking the variable and then click definition in the menu, Access will show you the way.

How to add new record with primary key constraint VB6.0 & SQL Server

I want to create a message box that will fire whenever the txt_unit is equals to the unit_no in the database. This is during the insertion of new data in the DB. I'm using VB6 and SQL Server 2005.
This is my code:
If txt_unit.Text = 'same unit number that exist in the DB' Then
MsgBox "Duplicate Record", vbCritical, "Duplicate"
txt_unit.Text = ""
txt_unit.SetFocus
This will save time for the user during data input.
Thanks in advance
After browsing the web for answers, I saw this site that explains the error handling in VB6.
I can finally move on now. Here's the code:
On Error GoTo err_CmdAdd_Click
con.Open _
"Provider = sqloledb;" & _
"Data Source=server;" & _
"Initial Catalog=database;" & _
"User ID=username;" & _
"Password=password;"
If img_edit.BorderStyle = 1 Then
Set rs = con.Execute("insert into a_owner values('" & txt_unit.Text & "', " _
& " '" & txt_tower.Text & "' )")
MsgBox "Record added successfully...", vbInformation
ado1.Refresh
exit_err_CmdAdd_Click:
Exit Sub
err_CmdAdd_Click:
MsgBox "Duplicate Record!", vbCritical, "Duplicate"
txt_unit.Text = ""
txt_unit.SetFocus
End If