 |
JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1617
|
Re: Null Record
Well... you could write a routine that handled it (like if you're checking numerous fields all over the place)
Otherwise it's just:
If IsNull(recordset.field(0)) Then
variable/object = ""
Else
variable/object=recordset.field(0)
End If
|
But as you can tell... it adds a lof of lines to the program, since it would have to be done for every field where the field could be null.
' In a module
Public function NullField(byref theField as ADODB.Field) as string
' Either
If IsNull(theField) Then
NullField=""
Else
Nullfield=theField
End If
' OR
Nullfield=""
If Not IsNull(theField) Then
Nullfield=theField
End If
end function
|
____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)
|
|
06-08-2005 at 05:59 PM |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: Null Record
Just an addition JLRodger's hint.
You can spare some rows by using the IIf function.
txtFamilyName.Text = IIf(Not IsNull(rs.Fields("FamilyName").Value), rs.Fields("FamilyName").Value, vbNullString)
txtName.Text = IIf(Not IsNull(rs.Fields("Name").Value), rs.Fields("Name").Value, vbNullString)
txtBirthDate.Text = IIf(Not IsNull(rs.Fields("BirthDate").Value), rs.Fields("BirthDate").Value, vbNullString)
txtBirthCity.Text = IIf(Not IsNull(rs.Fields("BirthCity").Value), rs.Fields("BirthCity").Value, vbNullString)
txtAddress.Text = IIf(Not IsNull(rs.Fields("Address").Value), rs.Fields("Address").Value, vbNullString) | ...but in this way the rows are very long and hard.readable.
Another method I use is to detect and bypass that specific errorPrivate Sub LoadMyFields()
On Error GoTo ErrRoutine
txtFamilyName.Text = rs.Fields("FamilyName").Value
txtName.Text = rs.Fields("Name").Value
txtBirthDate.Text = rs.Fields("BirthDate").Value
txtBirthCity.Text = rs.Fields("BirthCity").Value
txtAddress.Text = rs.Fields("Address").Value
Exit Sub
ErrRoutine:
If Err.Number = 94 Then Resume Next
End Sub |
Hope it helps
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
06-08-2005 at 10:52 PM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: Null Record
When working with text fields, no need for cheking the null value, just add empty string to it
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
08-08-2005 at 12:26 AM |
|
|
JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1617
|
Re: Null Record
If that's your actual code...
1) If there's more than 1 record, you can't delete message appears, if there's under 1 record (none), you allow them to delete
2) Regardless of what the user selects from the message box, it'll delete (or attempt to) (the if statement will be true always, unless vbOK = 0, if so, then it will never delete)
____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)
|
|
08-08-2005 at 07:53 AM |
|
|
elgy Level: Scholar
 Registered: 20-07-2005 Posts: 42
|
Re: Null Record
quote: JLRodgers wrote:
If that's your actual code...
1) If there's more than 1 record, you can't delete message appears, if there's under 1 record (none), you allow them to delete
2) Regardless of what the user selects from the message box, it'll delete (or attempt to) (the if statement will be true always, unless vbOK = 0, if so, then it will never delete)
sorry my mistake! it suppose to be
| if rs.recordcount = 0 then |
but still the recordcount always return -1
|
|
08-08-2005 at 08:05 AM |
|
|
elgy Level: Scholar
 Registered: 20-07-2005 Posts: 42
|
Re: Null Record
hei, i've found the answer!
in vb
rs.delete is can not delete the record if it contain a field or more that has null value, CMIIW
then to delete a record then
con.execute ("delete name from report where name="elgy")
i've tried and it works well.
|
|
09-08-2005 at 04:19 PM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: Null Record
Nope, I was reffering to yours:
CmbAlasan.Text = rs(9)
in rs(9) value =NULL
Lets clarify some things here... The reason why you are having -1 for recordcount is because you are using some cursor that doesnt support counting records.
Example, adOpenStatic and adOpenKeyset cursors support adBookmark property, therefore recordcount property will retrieve the exact count of records. On the other hand, adOpenForwardOnly and adOpenDynamic dont support it (if I remember well), so it will always return -1 as recordcount.
In your example, you can use
If rs.bof or rs.eof Then
MsgBox ("Unable to delete Record!"), vbCritical
Else
if MsgBox ("Are you sure want to delete this record?"), vbOKCancel)= vbok then
rs.Delete 'always got stuck here!
.......
endif
endif
no reason why it wont work, if you have created recordset with adequate CursorType and LockType.
At last, the method you have found can have unexpected results, if name field is not unique. For example, if you have 2 records that have name="elgy", then you will delete two records, not just the current one.
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
09-08-2005 at 08:06 PM |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: Null Record
The RecordCount property of a forward-only recordset (ADO default), and of some server-side cursors, always returns -1.
To get the correct record count you can assign it to an array by the GetRows method, and then count the array items by the UBound function. Sometimes, when the records are too many, this approach could cause a memory leak. And anyway, we often need the number before processing the recordset.
A trick is to send an SQL instruction before the real recordset retrieving, as following Dim rs As ADODB.Recordset
Dim sql As String, rsCount As Long
Set rs = New ADODB.Recordset
' set an instruction to get the record count
sql = "SELECT COUNT(*) FROM MYTABLE"
rs.CursorLocation = adUseServer
rs.Open sql, cn, adOpenForwardOnly, , adCmdText
' the retrieved recordset contains one single record with one single field
rsCount = rs(0)
rs.Close
sql = "SELECT * FROM MYTABLE ORDER BY ID"
' get the real recordset
rs.Open sql, cn, adOpenForwardOnly, , adCmdText
' ..... other code ..... |
When working with db engines which support multiple SQL intructions into a single query, such as MS SQL Server, you can group the two instructions, and pass them together to the engine, as following Dim rs As ADODB.Recordset
Dim sql As String, rsCount As Long
Set rs = New ADODB.Recordset
' set the two instructions together
' (notice the semicolon at the end of the first instruction)
sql = "SELECT COUNT(*) FROM MYTABLE; " _
& "SELECT * FROM MYTABLE ORDER BY ID"
rs.CursorLocation = adUseServer
rs.Open sql, cn, adOpenForwardOnly, , adCmdText
' the first recordset contains the record count
rsCount = rs(0)
' the second recordset effectively contain the records
Set rs = rs.NextRecordset |
This approach is a bit faster, as it requires only one single access to the server.
Hope it helps
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
10-08-2005 at 12:17 AM |
|
|
elgy Level: Scholar
 Registered: 20-07-2005 Posts: 42
|
Re: Null Record
Hei thanks for the reply and explanation!
but i think im not good enough yet, so hard to understand what you wrote. do you mind tell me bout what is cursor type, diferrences between 4 opt, and lock type as well. hehe.sorry for bother you.
|
|
12-08-2005 at 06:50 PM |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: Null Record
Well, it's not a simple matter...
"A cursor is a set of values that represents a query's result" (F. Balena - Programming Visual Basic 6 - Microsoft Press)
A cursor is the recordset itself, but we speak about "cursors" to specify where the the query has been executed to retrieve the records.
Consider a simple query: "SELECT * FROM MYTABLE ORDER BY DESCRIPTION". This instruction returns all the fields of all the records of the table MYTABLE, and then sort the record by the Description field contents.
You can execute it on the server (say, pass the instruction to the database, asking it "Please give me back only the matching records"). In this case, it's the database engine that filters the database, and provides only the matching records. If the server is located on a network computer, only the matching records will move thru the network cable.
Or else, you can execute the query on the client (the pc on which your VB app is currently running). In this case you will retrieve the whole database - a lot of data thru the network cable - and then you filter the database to search only the matching records.
The advantage of this method (client-side cursor) is that your query will probably be the only one executed on the whole database, while in the other case the server has to provide records to many clients together. And client-side cursors often expose some other properties, such as RecordCount.
The server-side method (server-side cursor) advantage is that often, in a network, the server is a more performant machine, so query execution is faster. And more, we have a lower traffic of data thru the cable. This is the meaning of the CursorLocation property.
The cursor type depends on the provider (the db engine) you use. The four avaliable types of ADO, from the fastest to the slowest, are Forward-Only, Static, Dynamic and Keyset. The slower ones offer more features, but we pay them by performance leak.
The lock is the way you block a record while you edit it, in order to avoid two users can edit the same record at the same time. There are four options, but in plain words we can say that from the optimistic to the pessimistic to the read-only, the lock is always less permissive. You can fnd more on the online help.
However I suggest youa good book about ADO or VB6, where all these things are better explained.
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
14-08-2005 at 03:20 AM |
|
|
wanie2005 Level: Protégé
 Registered: 23-11-2005 Posts: 5
|
Re: Null Record
i have problem when want to delete record from listview. the error is "either bof or eof is true, or the current record has been deleted. requested operation requires a current recod"..
my coding is:
Dim DB As Database
Dim rs As Recordset
Dim mntDel As Integer
Set DB = OpenDatabase(App.Path + "\iwanie.mdb")
' Open recordset (table: staff) in database DB
Set rs = New ADODB.Recordset
rs.Open "Select * From staff WHERE staff_id=" & lngStaffID, cn, adOpenKeyset, adLockOptimistic
mntDel = MsgBox("Are you sure to delete the entry for " & lvwAllStaff.ListItems.Item(ItemIndex).Text & "?", vbQuestion + vbYesNo, "Delete?")
If mntDel = vbYes Then
rs.Delete
rs.Update
frmAllStaffRecord.Show
End If
the error is highlighted at rs.Delete
even i also have the same error when trying to update record..my coding:
Set rs = New ADODB.Recordset
rs.Open "Select * From staff WHERE staff_id=" & lngStaffID, cn, adOpenKeyset, adLockOptimistic
With rs
.Fields("name") = txtStaffName
.Fields("ic") = txtStaffIC
.Fields("gender") = cboStaffGender
.Fields("address") = txtStaffAdd1
.Fields("state") = txtStaffState
.Fields("contact_no") = txtStaffContact
.Fields("mode") = cboStaffMode
.Fields("status") = cboStaffStatus
.Fields("password") = txtStaffPassword
.Update
.Close
End With
Set rs = Nothing
'sanitation
txtStaffName = ""
txtStaffIC = ""
cboStaffGender.ListIndex = -1
txtStaffAdd1 = -1
txtStaffState = -1
txtStaffContact = ""
cboStaffMode.ListIndex = -1
cboStaffStatus.ListIndex = -1
txtStaffPassword = ""
txtStaffName.Text = ""
txtStaffName.SetFocus
strStaffName = ""
lngStaffIC = 0
strStaffGender = ""
strStaffAddress = ""
strStaffState = ""
strStaffContact = ""
strStaffMode = ""
strStaffStatus = ""
strStaffPassword = ""
'updation is complete, now time to close the form
Unload Me
frmAllStaffRecord.Show
the error is always highlighted at .Fields("name") = txtStaffName
|
|
23-11-2005 at 10:18 PM |
|
|
JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1617
|
Re: Null Record
"" implies that the user entered information, but it was blank
null implies that the user didn't enter any information (or was never prompted to enter the field)
Like if you had a field of "sendnewsletter" and it was null, you'd know that they never requested nor denied a newsletter, but if it was "" they had the chance of requesting, but entered nothing.
____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)
|
|
24-11-2005 at 07:36 PM |
|
|
wanie2005 Level: Protégé
 Registered: 23-11-2005 Posts: 5
|
Re: Null Record
thank you for your help..thank you so much..
|
|
25-11-2005 at 04:03 PM |
|
|
newbie_1020 Level: Master

 Registered: 29-11-2005 Posts: 117
|
Re: Null Record
A trick is to send an SQL instruction before the real recordset retrieving, as following Dim rs As ADODB.Recordset
Dim sql As String, rsCount As Long
Set rs = New ADODB.Recordset
' set an instruction to get the record count
sql = "SELECT COUNT(*) FROM MYTABLE"
rs.CursorLocation = adUseServer
rs.Open sql, cn, adOpenForwardOnly, , adCmdText
' the retrieved recordset contains one single record with one single field
rsCount = rs(0)
rs.Close
sql = "SELECT * FROM MYTABLE ORDER BY ID"
' get the real recordset
rs.Open sql, cn, adOpenForwardOnly, , adCmdText
' ..... other code ..... |
once again,yronium never fails to amaze me.thanks a lot, i was browsing through old posts and found this.very helpfull.
____________________________
united we stand,divided we stand alone...
|
|
15-12-2005 at 07:25 AM |
|
|
|
|
 |
 |