473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

try to capture Null in vba when writing to a table

2
Hi i found the following code on the web to allow me to track changes. It works fine if there is
1 initially no data in the field
2 the field value changes to something else(text)

The Problem i have is when a user deletes the text in the box it crashes the code.
How can i capture the delete command and stop the code from crashing?
Function LogChanges(lngID As Long, Optional strField As String = "")
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim varOld As Variant
Dim varNew As Variant
Dim strFormName As String
Dim strControlName As String
Application.Echo False

varOld = Screen.ActiveControl.OldValue
varNew = Screen.ActiveControl.Value
' strFormName = Screen.ActiveForm.Name
strFormName = Screen.ActiveControl.Parent.Name
strControlName = Screen.ActiveControl.Name
Set dbs = CurrentDb()
Set rst = dbs.TableDefs("Audittrail").OpenRecordset

With rst
.AddNew
!FormName = strFormName
!ControlName = strControlName
If strField = "" Then
!FieldName = strControlName
Else
!FieldName = strField
End If
!recordid = lngID
!UserName = Environ("username")
If Not IsNull(varOld) Then
!OldValue = CStr(varOld)
End If
!NewValue = CStr(varNew)
.Update
End With

'clean up
rst.Close
Set rst = Nothing
dbs.Close
Set dbs = Nothing
Application.Echo True
End Function

thanks for any help

Kevin
Mar 6 '17 #1
3 1049
PhilOfWalton
1,430 Expert 1GB
Where does it crash?

You may need to check that LngID has a value and that VarNew has a value, and for that matter, all other variables that you are trying to add to the table.

The use of the Nz() function may help

Phil
Mar 6 '17 #2
Kevcar
2
thanks for reply Phil.
The code crashes on !NewValue = CStr(varNew) line.
I was thinking of putting a value in the cell that had been deleted, that way I would still have a record of the change.
if it better to put NZ into code
would I wrap the NZ function around the newvalue line?

thanks again

Kevin
Mar 7 '17 #3
PhilOfWalton
1,430 Expert 1GB
Two options

1)
Expand|Select|Wrap|Line Numbers
  1. !NewValue = CStr(Nz((varNew))
  2.  
2)
Expand|Select|Wrap|Line Numbers
  1. if Not IsNull(varNew) then
  2.     !NewValue = CStr(varNew)
  3. End If
  4.  
Phil
Mar 7 '17 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: MrDom | last post by:
I am wondering if someone can help solve this question I have a table in sql server 2000, I setup it using Enterprise manager. When I generate an SQL Script for this table it scripts as: ...
1
by: Daniel | last post by:
when writing out a file from .net, when is the file created? after the bytes are all written to the hard drive or before the bytes are written to the hard drive?
7
by: sql-db2-dba | last post by:
Does DB2 just fudge it when it is an empty table? Is there a "formula" for average row size when you have variable length records. Or you really have to know what your application is packing into...
2
by: ertanasan1 | last post by:
Hi, //Returns a new dataset with tree datatables. dsTemp.ReadXmlSchema(@"C:\mysch.xsd"); //Add a row to data table 0 DataRow dr = dsTemp.Tables.NewRow(); dr = "value1";...
3
by: TD | last post by:
I have an unbound form that adds and updates records to one table. I use an INSERT sql statment to add a new record and a UPDATE statement to update a record. I created a function named C2F to...
51
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
16
by: Jon | last post by:
Hello all, We have a strange problem with our app. If the database table to which the app is writing is open, the app hangs and just takes ages. Why would this be so? Thanks, Jon
2
by: Joe | last post by:
I'm binding to a column in a TemplateField. In some cases the join I have returns a null for an int field. I would like to specify a default value somehow so I don't end up with a null exception. ...
4
rajiv07
by: rajiv07 | last post by:
Hi to all I want to know How to update null value in table. Is any idea please. Thanks Regards RajivGandhi
2
by: =?Utf-8?B?S3VtYXI=?= | last post by:
I am using granados telnet client for connecting to the telnet and get the data from it. Every thing appears to be going smooth. But for some reason when I try to write the byte data to a string or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.