Using access vba to check if table column has null values - ms-access

I'm trying to find if the specified column has null values or no data.if null values are present in the column then gives the message box to user saying column contain nulls.
My vba
Dim sqlid As String
Dim rst As Recordset
Dim cdb As Database
Set cdb = CurrentDb
SQLEID = "SELECT * " & _
"FROM table_1 ;"
'
Set rst = cdb.OpenRecordset(sqlid , dbOpenSnapshot)
Do While Not rst.EOF
If IsNull(rst.Fields("column1").Value) Then
MsgBox "Has nulls"
End If
Loop
rst.Close
Set rst = Nothing
Set cdb = Nothing
But when i'm running this my access goes not responding. How do i check if the column has any nulls using vba

It hangs as your recordset is not being incremented, you need a MoveNext
Do While Not rst.EOF
If IsNull(rst.Fields("column1").Value) Then
MsgBox "Has nulls"
End If
rst.MoveNext
Loop
Simoco had a better suggestion for accomplishing this,
If DCount(1, "table_1", "IsNull([column1])")>0 Then
MsgBox "Has nulls"
End If

Related

Validate if particular value exists then In other column should not be null

I have two columns, If pricesource column has a value like 'Prior Value' and PO_DATE column has Nulls, Then i have to display the message "It has Nulls", I have tried something like this, but not getting proper output. Correct me
Dim rst As Recordset
Set rst = db.OpenRecordset("select pricesource from [Input Norm] where pricesource='Prior File'")
If rst And IsNull(PO_DATE) Then
MsgBox "PO_DATE HAS NULLS, Please check"
End If
You can use:
Dim rst As Recordset
Set rst = db.OpenRecordset("select PO_DATE from [Input Norm] where pricesource='Prior File'")
If rst.RecordCount > 0 Then
If IsNull(rst!PO_DATE.Value) Then
MsgBox "PO_DATE has Nulls, please check."
End If
End If
rst.Close
Set rst = Nothing

Search entire row and return column name in ms access using function

Could use a little help. Previously I have been using IIF statements to get this output but there are limitations to IIF and now am looking for a function (In MS Access) to meet the requirement.
Challenge: Need to search a row for a criteria (e.g. Title 00\Question..), if/when a match is found it returns the column name using a function withing a query (e.g. FieldCategorization(Title 00\Question). Click on links below to see table and desired output
Microsoft access table:
Desired output from query:
What I have so far searches the entire table, it doesn't seach a row-per-row basis:
Public Function FieldCategorization(TblName As String, Criteria As Long) As String
Set dbs = CurrentDb
Set rs = dbs.OpenRecordset(TblName)
Dim fld As DAO.Field
' MyValue = 224803 ' T00 = Title 00\Question\First Name Text
' MyValue = 224814 ' AB00 = Abbreviation 00
MyValue = Criteria
'MsgBox "TblName: " & TblName & vbCrLf & "Criteria: " & Criteria
rs.MoveFirst
' Move through each row in the recordset
'Do While Not rs.EOF
For Each fld In rs.Fields
If fld = MyValue Then
FieldCategorization = fld.Name
End If
Next fld
rs.MoveNext
'Loop
End Function
Just did something similar today. The example below is super simplified, but I think it has all the elements you're looking for.
dim fld as fields
dim rst, rstW as recordset
MyValue = "Title 00\Question"
Set dbs = currentdB
Set rst = dbs.openrecordset ("table1")
Set rstW = dbs.openrecordset ("table2")
rst.movefirst
Do while not rst.eof
for each fld in rst.fields
if me(fld.name) = MyValue then
rstW.addnew
rstW!Title00 = fld.name
rstW.update
end if
next fld
rst.movenext
Loop

delete duplicate records from query vba

I have built a query for duplicate records. I need to run some VBA code to check if the record is already in the table (query). If so, I need to delete the last record.
My form consists of a button with a value so that when you click the button, the data is insert into table
Dim qry As QueryDef
Set qry = CurrentDb.QueryDefs("duplicate records")
'which method do i use to see if the query got duplicate record'
With rstCategories
.MoveLast 0
End With
With rstCategories
rstCategories.Delete
End With
MsgBox "The problem already reported before!"
What I would do is run a quick query on your table:
Dim db as Database
Dim rec as Recordset
Set db = CurrentDB
Set rec = db.OpenRecordset ("SELECT * FROM MyTable WHERE MyValue = '" & Me.MyValue & "'")
If rec.EOF = true then
'No match found, so the value isn't in the table. Add it.
Else
'Match found. This value is already in the table.
MsgBox "This value is already in the table"
End If
ok i solved it
i created a query using the find duplicates query wizard.
then i insert this code in "form close"
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = Application.CurrentDb
Set rs = db.OpenRecordset("qry_Duplicates")
Do Until rs.EOF
rs.MoveFirst
rs.delete
rs.Close
Set rs = Nothing
Set rs = db.OpenRecordset("qry_Duplicates")
Loop
this code delete the entire row

Replacing null values for multiple columns in a table with a constant value

I am trying to run a script that replaces all null values in a table with a constant value in MS Access 2007. I am fully aware that this can be done using an update query. But the requirement that I need to do that for all columns in one click. I figured that find/replace can do this, but still I would rather minimize any manual work in the process.
I tried using this code but when I try to run it, it just doesn't do anything. It doesn't even trigger an error.
Sub replacingnulls()
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim fld As DAO.Field
Set dbs = CurrentDb
Set rs = dbs.OpenRecordset("testtable", dbOpenTable)
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
Do Until rs.EOF = True
rs.Edit
For Each fld In rs.Fields
If IsNull(fld.Value) Then
fld.Value = 555
End If
Next fld
rs.MoveNext
Loop
Else
MsgBox ("There are no records")
End If
rs.Close
End Sub
Call rs.Update to save the changes you made to the current record before moving to the next record.
Next fld
rs.Update ' <-- add this
rs.MoveNext
Instead of looping through the table rows and then through each field within each row, you could execute one UPDATE per field.
Const cstrTable As String = "testtable"
Dim db As DAO.Database
Dim fld As DAO.Field
Dim tdf As DAO.TableDef
Dim strUpdate As String
Set db = CurrentDb
Set tdf = db.TableDefs(cstrTable)
For Each fld In tdf.Fields
' Access will complain if you attempt an UPDATE on an autonumber field,
' so skip field with dbAutoIncrField attribute
If Not ((fld.Attributes And dbAutoIncrField) = dbAutoIncrField) Then
strUpdate = "UPDATE [" & cstrTable & "] SET [" & fld.Name & _
"] = 555 WHERE [" & fld.Name & "] Is Null;"
'Debug.Print strUpdate
db.Execute strUpdate, dbFailOnError
End If
Next
Beware that code will attempt to replace all Nulls with 555 regardless of the field's datatype. If all the fields are plain numbers, that may be fine. However a Date/Time field with Null would become Jul 8 1901 00:00:00. OTOH, your recordset approach would do the same thing ... so if it's OK there, it should be OK here, too.

Ms Access 2007 record set not auto filling into textbox

I have a module with a procedure inside that looks like this:
Public Sub OpenRecordset()
Dim qdf As QueryDef
Set qdf = CurrentDb.QueryDefs("QOff2")
qdf.Parameters(0).Value = [Forms]![Form]![Text10]
Dim db As Database
Dim rs As Recordset
Dim StrBusinesses As String
Set rs = qdf.OpenRecordset
If rs.EOF And rs.BOF Then
MsgBox ("No businesses exist for this Customer")
Exit Sub
Else
rs.MoveFirst
End If
StrBusinesses = ""
Do While Not rs.EOF
StrBusinesses = StrBusinesses & rs!Fnam & ", "
rs.MoveNext
Loop
rs.Close
StrBusinesses = Left(StrBusinesses, Len(StrBusinesses) - 2)
Forms!Form.Badge = StrBusinesses
Set rs = Nothing
End Sub
I am trying to get this module to input the query results into a textbox (forms!form.badge), but I can't seem to get it to do it like my 5 other dlookup functions. When I open up the module and push the green play button, it shows up on the correct textbox but also shows up on the other records as well. It also doesn't show up automatically, nor does it update as you enter in the parameters. Isn't a module supposed to help autofil numerous variables into a text box in place of dlookup for multiple values?
No. If Forms!Form!Badge is an unbound textbox, a value assigned to it will be shown identically for all records.
To individualize, you will need a lookup function which takes the ID or other unique value of the record as parameter(add to textbox):
=LookupBadges([Forms]![Form]![Text10])
Public Function LookupBadges(ByVal Value As Variant) As Variant
Dim db As Database
Dim qd As QueryDef
Dim rs As Recordset
Dim Businesses As String
Set db = CurrentDb
Set qd = db.QueryDefs("QOff2")
qd.Parameters(0).Value = Nz(Value)
Set rs = qd.OpenRecordset
If rs.RecordCount > 0 Then
rs.MoveFirst
Do While Not rs.EOF
Businesses = Businesses & rs!Fnam.Value & ", "
rs.MoveNext
Loop
End If
rs.Close
Businesses = Left(Businesses, Len(Businesses) - 2)
LookupBadges = Businesses
Set rs = Nothing
Set qd = Nothing
Set db = Nothing
End Function