|
-
Aug 19th, 2005, 03:33 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Problem with Code
Hello,
The following piece of code I am having an error in. I divided it to two parts to simplify it.
VB Code:
Dim sConnString As String
Dim rsTempRecordSet As New ADODB.Recordset
Dim cnTeamCMI As New ADODB.Connection
Dim sSQl As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\ÅÓáÇã\ÓØÍ ÇáãßÊÈ\ÈÑäÇãÌ ÇáÍÖæÑ æ ÇáÇäÕÑÇÝ\database.mdb;Jet OLEDB:Engine Type=5;"
With cnTeamCMI
.ConnectionString = sConnString
.ConnectionTimeout = 0
.CursorLocation = adUseClient
.Open strConnString
End With
sSQl = "SELECT [ÑÕíÏ ÇáÅÌÇÒÇÊ] FROM [ÇáÅÏÇÑí] WHERE [ÇÓã ÇáÚÇãá] = '" & lstMngment.List(lstMngment.ListIndex) & "'"
rsTempRecordSet.Open sSQl, cnTeamCMI, adOpenStatic, adLockReadOnly
With rsTempRecordSet
If .Fields("ÑÕíÏ ÇáÅÌÇÒÇÊ") >= 1 Then
cmdMGTHoliday.Enabled = True
Else
cmdMGTHoliday.Enabled = False
End If
End With
rsTempRecordSet.Close
Set rsTempRecordSet = Nothing
Set cnTeamCMI = Nothing
This part is fine. But I get the error in his part of code:
VB Code:
With cnTeamCMI
.ConnectionString = sConnString
.ConnectionTimeout = 0
.CursorLocation = adUseClient
.Open strConnString
End With
sSQl = "SELECT [ÍÖæÑ] FROM [ÊæÞíÊÇÊ ÇáÅÏÇÑí] WHERE [ÇÓã ÇáÚÇãá] = '" & lstMngment.List(lstMngment.ListIndex) & "' AND [ÊÇÑíÎ ÇáÏÎæá] = #" & Format(Now, "dd/mm/yy") & "#"
rsTempRecordSet.Open sSQl, cnTeamCMI, adOpenStatic, adLockReadOnly
With rsTempRecordSet
If .Fields("ÍÖæÑ") = "ÅÌÇÒÉ" Then
cmdMGTHoliday.Enabled = False
Else
cmdMGTHoliday.Enabled = True
End If
End With
rsTempRecordSet.Close
End Sub
It highlights this line: " If .Fields("ÍÖæÑ") = "ÅÌÇÒÉ" Then" with an error message telling me that either BOF or EIF is True, or the current record has been deleted, the process needs a current record. (The error message is in Arabic I tried to translate as accurate as I could). Any suggestions on how to solve this problem?
Thanx in advance !
-
Aug 19th, 2005, 03:41 AM
#2
Frenzied Member
Re: Problem with Code
It mostly means that the query didn't return any results, so before you actually start fetching data check if .EOF isn't true.
-
Aug 19th, 2005, 03:43 AM
#3
Thread Starter
Hyperactive Member
-
Aug 19th, 2005, 03:48 AM
#4
Frenzied Member
Re: Problem with Code
VB Code:
If not rsTempRecordSet.EOF then
' Do your data fetches here.
End if
-
Aug 19th, 2005, 03:51 AM
#5
Re: Problem with Code
VB Code:
sSQL = "SELECT [ÍÖæÑ] FROM [ÊæÞíÊÇÊ ÇáÅÏÇÑí] WHERE [ÇÓã ÇáÚÇãá] = '" & lstMngment.List(lstMngment.ListIndex) & "' AND [ÊÇÑíÎ ÇáÏÎæá] = #" & Format(Now, "dd/mm/yy") & "#"
With cnTeamCMI
.ConnectionString = sConnString
.ConnectionTimeout = 0
.CursorLocation = adUseClient
.Open strConnString
End With
With rsTempRecordSet
.Open sSQL, cnTeamCMI, adOpenStatic, adLockReadOnly
If Not (.EOF And .BOF) Then
If .Fields("ÍÖæÑ") = "ÅÌÇÒÉ" Then
cmdMGTHoliday.Enabled = False
Else
cmdMGTHoliday.Enabled = True
End If
End If
.Close
End With
-
Aug 19th, 2005, 03:59 AM
#6
Thread Starter
Hyperactive Member
Re: Problem with Code
But where is the execution statement for your code dee-u? And shouldn't we put this at the end of your code?
VB Code:
Set rsTempRecordSet = Nothing
Set cnTeamCMI = Nothing
-
Aug 19th, 2005, 04:01 AM
#7
Frenzied Member
Re: Problem with Code
not to mention .MoveNext if you have multiple records.
-
Aug 19th, 2005, 04:03 AM
#8
Thread Starter
Hyperactive Member
Re: Problem with Code
OK, here is the code that I have at the moment. But it doesn't do anything. (NO errors, nothing at all). By the way could you tell me where would I put the .movenext?
VB Code:
With cnTeamCMI
.ConnectionString = sConnString
.ConnectionTimeout = 0
.CursorLocation = adUseClient
.Open strConnString
End With
sSQl = "SELECT [ÍÖæÑ] FROM [ÊæÞíÊÇÊ ÇáÅÏÇÑí] WHERE [ÇÓã ÇáÚÇãá] = '" & lstMngment.List(lstMngment.ListIndex) & "' AND [ÊÇÑíÎ ÇáÏÎæá] = #" & Format(Now, "dd/mm/yy") & "#"
rsTempRecordSet.Open sSQl, cnTeamCMI, adOpenStatic, adLockReadOnly
With rsTempRecordSet
If Not (.EOF And .BOF) Then
If .Fields("ÍÖæÑ") = "ÅÌÇÒÉ" Then
cmdMGTHoliday.Enabled = False
Else
cmdMGTHoliday.Enabled = True
End If
End If
End With
rsTempRecordSet.Close
Set rsTempRecordSet = Nothing
Set cnTeamCMI = Nothing
-
Aug 19th, 2005, 04:11 AM
#9
Frenzied Member
Re: Problem with Code
Don't check on BOF, Begin of File is always true at the start 
VB Code:
With rsTempRecordSet
While not (.EOF)
If .Fields("ÍÖæÑ") = "ÅÌÇÒÉ" Then
cmdMGTHoliday.Enabled = False
Else
cmdMGTHoliday.Enabled = True
End If
.MoveNext
Wend
End with
This query is a bit weird as it probably never will have more then 1 result, it's only useful for more records.. like:
VB Code:
With rsTempRecordSet
While not (.EOF)
lstView.Listitems.Add,, .Fields("Username")
.MoveNext
Wend
End with
Kinda like that, it just moves the record up or down by one notch (depending how you look at it hehe). As soon as it hits the end EOF is set to true and thus the While loop ends.
-
Aug 19th, 2005, 04:12 AM
#10
Re: Problem with Code
 Originally Posted by ielashi
But where is the execution statement for your code dee-u? And shouldn't we put this at the end of your code?
VB Code:
Set rsTempRecordSet = Nothing
Set cnTeamCMI = Nothing
I just tried to fix your code, so it is still your code and not mine...
-
Aug 19th, 2005, 04:23 AM
#11
Thread Starter
Hyperactive Member
Re: Problem with Code
But I was wondering how come the first one worked? Anyways, here is the code I have right now. Still it doesn't function. I think it's because EOF is true so it doesn't process the If statement. Is there like a command to set it to false or something else that would fix this?
VB Code:
With cnTeamCMI
.ConnectionString = sConnString
.ConnectionTimeout = 0
.CursorLocation = adUseClient
.Open strConnString
End With
sSQl = "SELECT [ÍÖæÑ] FROM [ÊæÞíÊÇÊ ÇáÅÏÇÑí] WHERE [ÇÓã ÇáÚÇãá] = '" & lstMngment.List(lstMngment.ListIndex) & "' AND [ÊÇÑíÎ ÇáÏÎæá] = #" & Format(Now, "dd/mm/yy") & "#"
rsTempRecordSet.Open sSQl, cnTeamCMI, adOpenStatic, adLockReadOnly
With rsTempRecordSet
While Not (.EOF)
If .Fields("ÍÖæÑ") = "ÅÌÇÒÉ" Then
cmdMGTHoliday.Enabled = False
Else
cmdMGTHoliday.Enabled = True
End If
.MoveNext
Wend
End With
rsTempRecordSet.Close
Set rsTempRecordSet = Nothing
Set cnTeamCMI = Nothing
-
Aug 19th, 2005, 04:30 AM
#12
Frenzied Member
Re: Problem with Code
Ehh don't use the While statement for 1 record, just use the IF statement.
Something like this:
VB Code:
With rsTempRecordSet
If Not (.EOF)
If .Fields("ÍÖæÑ") = "ÅÌÇÒÉ" Then
cmdMGTHoliday.Enabled = False
Else
cmdMGTHoliday.Enabled = True
End If
Else
cmdMGTHoliday.Enabled = True
End if
End With
-
Aug 19th, 2005, 09:07 AM
#13
Thread Starter
Hyperactive Member
Re: Problem with Code
Sorry for late reply, I tried what you said, I am not getting any errors now, but it's not doing what I want. It skips the if statement that I have as EOF is always true. Here's the code I have at the moment:
VB Code:
With cnTeamCMI
.ConnectionString = sConnString
.ConnectionTimeout = 0
.CursorLocation = adUseClient
.Open strConnString
End With
sSQl = "SELECT [ÍÖæÑ] FROM [ÊæÞíÊÇÊ ÇáÅÏÇÑí] WHERE [ÇÓã ÇáÚÇãá] = '" & lstMngment.List(lstMngment.ListIndex) & "' AND [ÊÇÑíÎ ÇáÏÎæá] = #" & Format(Now, "dd/mm/yy") & "#"
rsTempRecordSet.Open sSQl, cnTeamCMI, adOpenStatic, adLockReadOnly
With rsTempRecordSet
If Not (.EOF) Then
If .Fields("ÍÖæÑ") = "ÅÌÇÒÉ" Then
cmdMGTHoliday.Enabled = False
Else
cmdMGTHoliday.Enabled = True
End If
Else
cmdMGTHoliday.Enabled = True
End If
End With
rsTempRecordSet.Close
Set rsTempRecordSet = Nothing
Set cnTeamCMI = Nothing
-
Aug 19th, 2005, 09:26 AM
#14
Frenzied Member
Re: Problem with Code
It skips the if statement that I have as EOF is always true
That's why there is a Else... statement. If there is no record returned you can put the code in the else statement. I put cmdMGTHoliday.enabled = true there as I assumed that would be the default.
-
Aug 19th, 2005, 09:34 AM
#15
Thread Starter
Hyperactive Member
Re: Problem with Code
I understand what you mean. But the problem is that I'm POSITIVE there are records matching my query.
-
Aug 19th, 2005, 09:45 AM
#16
Fanatic Member
Re: Problem with Code
Why don't you stick a
MsgBox rsTempRecordSet.recordcount
after you run the sql statement just to see if it is returning a record or not.
Here's to us!
Who's like us?
Darned few, and they're all dead!
-
Aug 19th, 2005, 09:45 AM
#17
Thread Starter
Hyperactive Member
Re: Problem with Code
What if we use WHERE EXISTS in the SQL SELECT statement, would that work?
-
Aug 19th, 2005, 09:49 AM
#18
Thread Starter
Hyperactive Member
Re: Problem with Code
Thanx for the advice demotivator, I'll give it a shot
-
Aug 19th, 2005, 10:06 AM
#19
Thread Starter
Hyperactive Member
Re: Problem with Code
OK, I seem to understand the problem and it's almost fixed. I'm trying now to make an if statement for multiple database fields, I think my syntax is incorrect. Here it is:
VB Code:
If .Fields("ÍÖæÑ") = "ÅÌÇÒÉ" And .Fields("ÊÇÑíÎ ÇáÏÎæá") = "'" & Format(Now, "dd/mm/yy") & "'" Then
cmdMGTHoliday.Enabled = True
Else
cmdMGTHoliday.Enabled = False
End If
Any ideas about the basic syntax for the if statement above would be greatly appreciated. By the way, do all the fields in the if statement above must be in the SELECT SQL statement?
-
Aug 19th, 2005, 10:14 AM
#20
Fanatic Member
Re: Problem with Code
That looks ok to me. And yes, the fields have to be in the sql statement.
You may also want to check if the fields you have retreived are null before doing any work with them.
ie:
If isnull("youfield") = false then
'do you stuff
Else
'can't do your stuff, the field is null
End If
Here's to us!
Who's like us?
Darned few, and they're all dead!
-
Aug 19th, 2005, 10:27 AM
#21
Member
Re: Problem with Code
Hope you guys don't mind if I hop in 
 Originally Posted by Devion
Don't check on BOF, Begin of File is always true at the start 
Do you mind explaining this one?
BTW IMHO the proper way of determining whether the result returned by a query is empty is by cheking both EOF and BOF not just BOF nor EOF 
To ielashi
if you are assigning the value of your fields to a textbox or other control then demotivater is quite correct you should check the value of your fields whether it is NULL or not before passing it to a textbox control etc.., but another method would be to just add a vbNullString or "" (empty string) and eliminate the If block.
BTW Format will already return string why do you need to add a single quote (') in the result?
-
Aug 19th, 2005, 10:29 AM
#22
Thread Starter
Hyperactive Member
Re: Problem with Code
Thank you. Actually one of the fields of my if statement wasn't in the SELECT SQL statement, so I had to write it again. But there seems to be a problem in it which puzzles me. It just tells me that are no records for it although there is in the database data matching my query. Here is my statement:
VB Code:
sSQl = "SELECT [ÍÖæÑ] FROM [ÊæÞíÊÇÊ ÇáÅÏÇÑí] WHERE [ÇÓã ÇáÚÇãá] = '" & lstMngment.List(lstMngment.ListIndex) & "' AND [æÞÊ ÇáÏÎæá] = #" & Format(Now, "dd/mm/yy") & "#"
Any suggestions?
-
Aug 19th, 2005, 10:33 AM
#23
Thread Starter
Hyperactive Member
Re: Problem with Code
DENNIS DVR, I can't really understand what you mean. Can you put it an example code to make more clearer?
BTW Do the fields have to actually be null so I can work with them?
-
Aug 19th, 2005, 10:34 AM
#24
Fanatic Member
Re: Problem with Code
Are you sure you want to be searching the database for the List Index?
[edit]Whoops, nevermind, misread your code[/edit]
Last edited by demotivater; Aug 19th, 2005 at 10:37 AM.
Here's to us!
Who's like us?
Darned few, and they're all dead!
-
Aug 19th, 2005, 10:36 AM
#25
Member
Re: Problem with Code
Are you sure that [æÞÊ ÇáÏÎæá] is a date datatype? and the format is dd/mm/yy?
BTW you are asking example? to what?
-
Aug 19th, 2005, 10:38 AM
#26
Thread Starter
Hyperactive Member
Re: Problem with Code
No no, you see, it's looking for the name I clicked on in the listbox. This part's fine. The problem I think is in the last part, with the date thing.
-
Aug 19th, 2005, 10:40 AM
#27
Thread Starter
Hyperactive Member
Re: Problem with Code
Yes, it is a DateTime field. I was asking example regarding the vbnullstring. And do the fields have to be null so that I can work them?
-
Aug 19th, 2005, 10:49 AM
#28
Thread Starter
Hyperactive Member
Re: Problem with Code
I substituted the fields in the SQL statement with relevant ones in English to make things a little clearer.
VB Code:
sSQl = "SELECT [Holiday] FROM [Employee Timings] WHERE [Employee Name] = '" & lstMngment.List(lstMngment.ListIndex) & "' AND [Punchin Time] = #" & Format(Now, "dd/mm/yy") & "#"
-
Aug 19th, 2005, 11:37 AM
#29
Thread Starter
Hyperactive Member
Re: Problem with Code
Could someone please tell me what's wrong with this if statement, been trying to work on it forever, I just don't know what's wrong. There are no errors formaed, but the value of the field I entered did not match. Needs to be rephrased I guess. Anyone have any ideas?
VB Code:
If .Fields("ÊÇÑíÎ ÇáÏÎæá") = "#" & Format(Now, "dd/mm/yyyy") & "#" Then
cmdMGTHoliday.Enabled = False
Else
cmdMGTHoliday.Enabled = True
End If
-
Aug 19th, 2005, 11:42 AM
#30
Member
Re: Problem with Code
 Originally Posted by ielashi
Yes, it is a DateTime field
But how about the format is it really dd/mm/yy?
 Originally Posted by ielashi
I was asking example regarding the vbnullstring
If you are passing the value of your fields to a textbox control etc.. then you could do something like
VB Code:
Text1.Text = vbNullString & .Fields("YourField").Value 'It will not give you an error even if .Fields("YourField").Value is NULL
This one will throw an error if .Fields("YourField").Value is NULL
VB Code:
Text1.Text = .Fields("YourField").Value
But as far as I can see it you are not passing the value of your fields to a textbox control etc.
 Originally Posted by ielashi
And do the fields have to be null so that I can work them?
No, it doesn't have to be NULL to be able for you to work with then 
and did you removed the single quote in your IF statement? i.e.
VB Code:
If .Fields("ÍÖæÑ") = "ÅÌÇÒÉ" And .Fields("ÊÇÑíÎ ÇáÏÎæá") = Format(Now, "dd/mm/yy") Then
-
Aug 19th, 2005, 11:47 AM
#31
Thread Starter
Hyperactive Member
Re: Problem with Code
Actually, I modified the SELECT SQL statement minorly and therefore the if statement has changed. Please take a look at my previous post and inform me if you see any error in it.
-
Aug 19th, 2005, 11:49 AM
#32
Member
Re: Problem with Code
I don't think you need a pound or number sign there
-
Aug 19th, 2005, 11:57 AM
#33
Thread Starter
Hyperactive Member
Re: Problem with Code
OK, I removed them and the line now looks like this:
VB Code:
If .Fields("ÊÇÑíÎ ÇáÏÎæá") = " & Format(Now, "dd/mm/yyyy") & " Then
But do you know that ASCI thing which replaced this double apostrophe " because it's giving me an error because of that.
-
Aug 19th, 2005, 12:01 PM
#34
Re: Problem with Code
Take the quotes out.... you do not need them....
VB Code:
If .Fields("ÊÇÑíÎ ÇáÏÎæá") = Format(Now, "dd/mm/yyyy") Then
Tg
-
Aug 19th, 2005, 12:04 PM
#35
Member
Re: Problem with Code
Post #30
-
Aug 19th, 2005, 12:16 PM
#36
Thread Starter
Hyperactive Member
Re: Problem with Code
Tried removing quotes but still doesn't work
-
Aug 19th, 2005, 01:25 PM
#37
Re: Problem with Code
What doesn't work? Why? What? Where?
[snarky=on]Saying it doesn't work does the rest of us no good. That's walking into the doctor's office and say it hurts. He's going to ask what hurts. Ok, so it doesn't work? Well, why not? What kind of errors do you get? HOW do you know it isn't working?[/snarky]
Tg
-
Aug 19th, 2005, 02:14 PM
#38
Thread Starter
Hyperactive Member
Re: Problem with Code
What I meant was that when I removed the quotes from the If statement it doesn't work. It doesn't show any errors because it is skipped by another if statement because it doesn't math the field database. But no problem, I made another alternative. Instead of making the if statement on this datetime field, I made it based on another string field. It works now, but thank you people so much for your help.
-
Aug 19th, 2005, 07:39 PM
#39
Re: [RESOLVED] Problem with Code
Usually I do it this way...
VB Code:
If DateValue(.Fields("ÊÇÑíÎ ÇáÏÎæá")) = DateValue(Date) Then
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|