when my table is empty i always encounter this error what will do.
Printable View
when my table is empty i always encounter this error what will do.
That do you want the application to do?
When I open a recordset before I do anything with it I check for .BOF and .EOF like this:
VB Code:
rs.Open strSQL if Not rs.BOF and Not rs.EOF then 'Do something with the result set else 'Something to do if nothing is there end if if not rs is Nothing then if rs.State then rs.Close set rs = Nothing end if
i try your code but still have error... here's my code
VB Code:
Private Sub cmdSave_Click() If CheckTextBox(txtDepTitle, "Invalid 'DEPARTMENT Title' value. This field is required, please enter a value.") = False Then StartTxt txtDepTitle Else With tbDepartment .MoveFirst .Find "DepartmentName = '" & txtDepTitle & "'" If .EOF = True Then .AddNew .Fields(0) = txtDepTitle .Update .Requery End If End With ans = MsgBox("New Deparment entry successfully saved to the record." & vbNewLine & "Do You Want To Add Another Record?", vbYesNo, "Titus School Of Multimedia") If ans = vbNo Then Unload Me End If End If End Sub
from ur code, try this one :
Code:With tbDepartment '// Assumming this is an open recordset
If .EOF or .BOF Then
Msgbox "No Record(s)",64,App.Title
.Close
Exit Sub
End If
.MoveFirst
.Find "DepartmentName = '" & txtDepTitle & "'"
If .EOF = True Then
End If
End With
If there are no records then the .MoveFirst will cause the error. You need to test for BOF/EOF before you try and move in a recordset. Also when the recordset is first opened there is no need to issued the .MoveFirst method you are already at the first record.
Quote:
Originally Posted by mheiy
Why are you doing a .MoveFirst on an empty table? Why are you trying to Search an empty table?Quote:
Originally Posted by mheiy
Either and both of those will result in an EOF error.
what if there's a record already...
Then, if what you said in your first postQuote:
Originally Posted by mheiy
is true, you shouldn't get the error.Quote:
Originally Posted by mheiy
ok at first my table is empty then im going to add new one... and add again other one
So, do you continue to get the .EOF/.BOF error when the table is not empty?
you dont understand me...
what im trying to say is when my table is empty i got an error.. then you say that why do i need to .Movefirst and search... i need the .movefirst when my table is not empty.. got it
As mentioned several times, you can only do a MoveFirst or Find if there are records.. check EOF/BOF (as shown already - Just a noob's example is good) to see if there are records, and only do the .MoveFirst/Find if there are.
Code:
Option Explicit
Private c As ADODB.Connection
Private r As ADODB.Recordset
Private Function AddNewRec(IdentifyValue As String) As Boolean
On Error Resume Next
'No need for .MoveFirst or Looping throught recordset _
if u just want to Add or Update record !!!
With r
.Open "select [Fields] from [Table] where [FieldIdentify]='" & _
IdentifyValue & "';", c, adOpenDynamic, adLockOptimistic
If (.EOF Or .BOF) Then .AddNew '/. No Record(s) that match _
Criteria or table is Empty - AddNew
.Fields(0).Value = "Some Value"
.Update '/. if found match record from where clause _
perform - Update
.Close
End With
AddNewRec = True
If (Err <> 0) Then
AddNewRec = False
On Error GoTo 0
Err.Clear
End If
End Function
Let's start with the code you use to open the record set. Can we see what you are doing? As alrready said if the recordset is empty and you do a .MoveFirst then you get an error. If the recordset is empty and you do a .Seach then you get an error. You can not perform either if the recordset is empty. So before you do either of them check for BOF/EOF. It either condition is true then you have and empty recordset and don't perform a .MoveFirst or .Search. just go to the add new if that is what you wnat to do.
In trruth you should start to learn SQL language to reteive the data you wnat not just open a table and look for stuff in it.
how can i know if there's an existing record already
When you open the recordset you perform this code (as I showed you in post number 2):
VB Code:
if Not tbDepartment.BOF and Not tbDepartment.EOF then Do you stuff here else MsgBox "No Records returned NOTHING TO SEARCH FOR HERE!" end if
Better yetQuote:
Originally Posted by GaryMazzone
Then all you would have to do is a SELECT COUNT(*) FROM tablename, and badda bing, you would know how many records are in your table.Quote:
Originally Posted by GaryMazzone
And badda bing, that required two trips to the database.....
-tg
Not if all he wanted was a count of the records.Quote:
Originally Posted by techgnome
i've alredy your code and its running now but when i need to add another record its not adding. what will i do
Whose code are you running?
What happens when you try to add another record?
VB Code:
if Not tbDepartment.BOF and Not tbDepartment.EOF then Do you stuff here else MsgBox "No Records returned NOTHING TO SEARCH FOR HERE!" end if
the msgbox "no records forund" is always shown
The code I should you will not add a new record to the table. It just shows what to do if no records are returned from the table.
What is the code you are using to add a new record? (I really hope it's not .AddNew and .Update I still think the SQL statements are better to learn).