I am trying to display error message in MS Access which will get the error from one table.
There is a table, having only one column which is having error records. I need to display all the records from table in message box of ms access.
I know how to get the message box but unable to find how to fetch records in the msg box
MsgBox "Errors are :" & vbCr & _
"E.g. 000123", vbCritical + vbOKOnly
How can i get the values from table?
Add this sub in your form or a stand alone module :
Public Sub DisplayErrors()
Dim RS As Recordset
Dim strErrors as string
Set RS = CurrentDb.OpenRecordset("SELECT * FROM MyErrorTable")
If Not RS.BOF Then
While Not RS.EOF
strErrors = strErrors & RS!MyErrorField & vbCrLf & vbCrLf
RS.MoveNext
Wend
End If
RS.Close
Set RS = Nothing
MsgBox "Errors are : " & vbCrLf & strErrors , vbCritical + vbOKOnly
End Sub
Replace MyErrorTable and MyErrorField with the name of your table and it's unique field containing the errors.
Then call your sub from anywhere with :
DisplayErrors
Related
Is there a way wherein I will be able to compare data types of 2 tables in MS Access? I have one table with 66 columns with specified names (Like ID, Name, ETc...) and another table that will be autogenerated based on an extract with the same number of columns but the field names default (F1, F2, F3, ... F66) so would like to check if there is a way in access that I can compare the data types of 66 columns from both tables? Thanks Much! :D
You can use some VBA to loop the Fields collection of each table. Something like this should get you started:
Sub sCompareTables(strTable1 As String, strTable2 As String)
On Error GoTo E_Handle
Dim db As DAO.Database
Dim tdf1 As DAO.TableDef
Dim tdf2 As DAO.TableDef
Dim lngLoop1 As Long
Set db = CurrentDb
Set tdf1 = db.TableDefs(strTable1)
Set tdf2 = db.TableDefs(strTable2)
If tdf1.Fields.Count = tdf2.Fields.Count Then
For lngLoop1 = 0 To tdf1.Fields.Count - 1
If tdf1.Fields(lngLoop1).Type = tdf2.Fields(lngLoop1).Type Then
Debug.Print "Match: " & tdf1.Fields(lngLoop1).name & vbTab & tdf2.Fields(lngLoop1).name & vbTab & fDatatype(tdf1.Fields(lngLoop1).Type)
Else
Debug.Print "No Match: " & tdf1.Fields(lngLoop1).name & vbTab & tdf2.Fields(lngLoop1).name & vbTab & fDatatype(tdf1.Fields(lngLoop1).Type) & "|" & fDatatype(tdf2.Fields(lngLoop1).Type)
End If
Next lngLoop1
Else
Debug.Print "Field counts do not match"
End If
sExit:
On Error Resume Next
Set tdf1 = Nothing
Set tdf2 = Nothing
Set db = Nothing
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "sCompareTables", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Sub
Function fDatatype(lngDatatype As Long) As String
On Error GoTo E_Handle
Select Case lngDatatype
Case 4
fDatatype = "Long"
Case 8
fDatatype = "Date"
Case 10
fDatatype = "Text"
Case 12
fDatatype = "Memo"
Case Else
fDatatype = "Unknown"
End Select
fExit:
On Error Resume Next
Exit Function
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "fDatatype", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume fExit
End Function
The function fDatatype that I've created does not list all of the available datatypes that are available - you can see a full list by pressing F2 to bring up the Object Browser, and searching for something like dbText to see all members of the DAO.DataTypeEnum class.
I looks like when i use one Combo-box as a criteria works just fine however using more than that though following the same steps doesn't work . I don't use SQL i do use the Design view though.
How to make all combo boxes works together to provide the needed criteria.
If you are looking to filter a form or list box using data selected from several combo boxes, then you will need to build up the RowSource "on the fly" based on the selections made.
Below is some sample code that uses selections from 2 combo boxes (cboCountry and cboRMZone) to create the RowSource for a list box (lstCountry):
Private Sub cboCountryZone_AfterUpdate()
Call sSearchMultiple
End Sub
Private Sub cboRMZone_AfterUpdate()
Call sSearchMultiple
End Sub
Private Sub Form_Load()
Call sSearchMultiple
End Sub
Private Sub sSearchMultiple()
On Error GoTo E_Handle
Dim strSQL As String
If Not IsNull(Me!cboCountryZone) Then strSQL = strSQL & " AND CountryZone_PK=" & Me!cboCountryZone
If Not IsNull(Me!cboRMZone) Then strSQL = strSQL & " AND RMZone_PK=" & Me!cboRMZone
If Left(strSQL, 4) = " AND" Then
strSQL = " WHERE " & Mid(strSQL, 6)
End If
If Len(strSQL) > 0 Then
Me!lstCountry.RowSource = "SELECT CountryName FROM dbo_svr_Country " & strSQL & " ORDER BY CountryName ASC;"
Else
Me!lstCountry.RowSource = "SELECT CountryName FROM dbo_svr_Country ORDER BY CountryName ASC;"
End If
sExit:
On Error Resume Next
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "Form3!sSearchMultiple", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Sub
Regards,
I have the following the detect duplicate product name when user enters a new record.
Private Sub ProdName_BeforeUpdate(Cancel As Integer)
Dim Product As String
Dim stLinkCriteria As String
Dim rsc As DAO.Recordset
Set rsc = Me.RecordsetClone
Product = Me.ProdName.value
stLinkCriteria = "[ProdName]=" & "'" & Product & "'"
If DCount("ProdName", "ProdProduct", stLinkCriteria) > 0 Then
Me.Undo
MsgBox "Warning duplicate entry " _
& Product & " has already been entered." _
& vbCr & vbCr & "You will now be taken to the record.", vbInformation _
, "Duplicate Information"
'Go to record of original product name
rsc.FindFirst stLinkCriteria
Me.Bookmark = rsc.Bookmark
End If
Set rsc = Nothing
End Sub
The code checks and finds duplicate but after it displays the following error and doesn't go to the orginal record:
Run-time error '3420'
Object invalid or no longer set
Please can someone help me get it right?
Try to avoid the undo (or move it to the end) and do cancel the update:
If DCount("ProdName", "ProdProduct", stLinkCriteria) > 0 Then
Cancel = True
MsgBox "Warning duplicate entry " _
& Product & " has already been entered." _
& vbCr & vbCr & "You will now be taken to the record.", vbInformation _
, "Duplicate Information"
'Go to record of original product name
rsc.FindFirst stLinkCriteria
Me.Bookmark = rsc.Bookmark
End If
And you may even skip the DCount and must use the Text property:
Product = Me!ProdName.Text
stLinkCriteria = "[ProdName]=" & "'" & Product & "'"
rsc.FindFirst stLinkCriteria
Cancel = Not rsc.NoMatch
If Cancel = True Then
MsgBox "Warning duplicate entry " _
& Product & " has already been entered." _
& vbCr & vbCr & "You will now be taken to the record.", vbInformation _
, "Duplicate Information"
'Go to record of original product name
Me.Bookmark = rsc.Bookmark
End If
The Run-time Error 3420, Object Invalid or No Longer Set occurs when the form status is dirty, you can simply set the form dirty to false before .findFirst call.
If Me.Dirty Then
Me.Dirty = False
End If
Me.Recordset.FindFirst stLinkCriteria
I am having some problems with my Access d-Base. I will try to explain what I want to achieve:
I have a continuous form based on a query with a lot of records. Users have to scan these records and If needed send a selection of these records to the concerning customer. Therefore I have used a textbox in my form combined with the following query column:
Expr1: (IIf([Forms]![frm_POOpenOrdersPerVendor]![partnrFilter] Is Null,True,[po_partnr]=[Forms]![frm_POOpenOrdersPerVendor]![partnrFilter]))
And criteria is: <>False
This works perfect in my form. After my filter is set I want to e-mail the results in the body of my email (Outlook). To do this I use:
Option Compare Database
Public Sub Export_PO()
On Error GoTo EH
'Recordset variables
Dim db As Database
Dim rstOpenPurchaseOrders As Recordset
Dim strSQL As String
Dim strSubject As String
Dim strBody As String
Dim strAddresses As String
Set db = CurrentDb()
strSQL = "SELECT * FROM qry_POOpenOrdersPerVendor;"
Set rstOpenPurchaseOrders = db.OpenRecordset(strSQL, dbOpenDynaset)
If Not rstOpenPurchaseOrders.EOF Then
strBody = "blah blah blah" & vbCrLf & vbCrLf
strBody = strBody & "Partnumber" & " | " & "Vendor Partnumber" & " | " & "Promise Date" & vbCrLf
strBody = strBody & "===========================================================" & vbCrLf
rstOpenPurchaseOrders.MoveFirst
Do While Not rstOpenPurchaseOrders.EOF
strBody = strBody & rstOpenPurchaseOrders![po_partnr] & Chr(9) & rstOpenPurchaseOrders![po_vendor_part] & Chr(9) & rstOpenPurchaseOrders![po_prom_date] & vbCrLf
rstOpenPurchaseOrders.MoveNext
Loop
End If
strSubject = "Here's my report!"
strAddresses = "look.look#nmhg.com"
DoCmd.SendObject acSendNoObject, , acFormatTXT, asAdresses, , , strSubject, strBody, True
Exit Sub
EH:
MsgBox Err.Number & " " & Err.Description
Exit Sub
End Sub
Separately my codes work fine (both). If I want to combine them I get the error "3061 Too few parameters. Expected 1."
Anybody?
Thanks in advance!
Your Query qry_POOpenOrdersPerVendor - required one argument to be passed either via a Form or just by prompting for a value. In VBA if you wish to use a precompiled query that expects a parameter you will bump into this error. So you need to use the main Query to be used in the VBA NOT the name of the Query.
I use the DAO method Execute to delete some records. If this fails this is clear by checking RecordsAffected (it will be 0). But is it possible to get the error message (for instance, to log or to show to the user)? I've try to delete the records by hand in the Table grid I get a clear dialog message, e.g. "The record cannot be deleted or changed because tabel x includes related records".
Include the dbFailOnError option with the Execute method to capture your DELETE errors. Without dbFailOnError, your DELETE can fail silently.
Relying on RecordsAffected to indicate a DELETE failure can be misleading. For example if your DELETE includes "WHERE Sample=5", and there is no row with a Sample value of 5, RecordsAffected will be 0. That is not an error to the database engine.
In the following sample, the DELETE fails because there is a relationship, with referential integrity enforced, between tblParent and tblChild. So the message box says "The record cannot be deleted or changed because table 'tblChild' includes related records".
Public Sub DeleteFailure()
Dim strSql As String
Dim strMsg As String
Dim db As DAO.Database
On Error GoTo ErrorHandler
strSql = "DELETE FROM tblParent WHERE id = 1;"
Set db = CurrentDb
db.Execute strSql, dbFailOnError
ExitHere:
On Error GoTo 0
Debug.Print "RecordsAffected: " & db.RecordsAffected
Set db = Nothing
Exit Sub
ErrorHandler:
strMsg = "Error " & Err.Number & " (" & Err.Description _
& ") in procedure DeleteFailure"
MsgBox strMsg
GoTo ExitHere
End Sub
Update: Here is a revised ErrorHandler to accommodate multiple errors triggered by a DAO operation.
ErrorHandler:
Dim errLoop As Error
Debug.Print "Errors.Count: " & Errors.Count
For Each errLoop In Errors
With errLoop
strMsg = "Error " & Err.Number & " (" & _
Err.Description & _
") in procedure DeleteFailure"
End With
MsgBox strMsg
Next
Set errLoop = Nothing
GoTo ExitHere
It should be possible to use DBEngine errors: http://msdn.microsoft.com/en-us/library/bb177491(office.12).aspx