PC Review


Reply
Thread Tools Rate Thread

DAO coding question opening a record set.

 
 
Aldred@office
Guest
Posts: n/a
 
      26th Sep 2008
Hi,
I have a piece of codes like this:

Private Sub Submit_Click()

Dim dbs As Database
Dim qdf As QueryDef
Set dbs = CurrentDb()
Set qdf = dbs.CreateQueryDef(CheckPartNum, "Select PartNum from PartNum
where PartNum='" & PNum1 & "'")
Set rst = dbs.OpenRecordset("PartNum") 'PartNum is a table name

If Not rst.EOF Then
MsgBox ("Found")
Else
MsgBox ("Not Found")
End If
End Sub


I'd like to check if the submitted partnum is already existed in the PartNum
Table. However, how could I have the Select query executed? With
OpenRecordset("PartNum") it just return the first record in the PartNum.
How could fix this?

Thanks.

 
Reply With Quote
 
 
 
 
Michel Walsh
Guest
Posts: n/a
 
      26th Sep 2008
If you just want to check for existence, you can test the count:

if 0 = DCount("*", "PartNum" , "PartNum = " & PNum ) then
MsgBox "Not found"
else
MsgBox "found"
end if


Sure, you can also use your query, but with SELECT COUNT(*) FROM ... and
open the recordset from the querydef:


Set rst = qdf.OpenRecordset( type, options, lockedits)


... note that the arguments are all optional.





Vanderghast, Access MVP



"Aldred@office" <aldred> wrote in message
news:(E-Mail Removed)...
> Hi,
> I have a piece of codes like this:
>
> Private Sub Submit_Click()
>
> Dim dbs As Database
> Dim qdf As QueryDef
> Set dbs = CurrentDb()
> Set qdf = dbs.CreateQueryDef(CheckPartNum, "Select PartNum from PartNum
> where PartNum='" & PNum1 & "'")
> Set rst = dbs.OpenRecordset("PartNum") 'PartNum is a table name
>
> If Not rst.EOF Then
> MsgBox ("Found")
> Else
> MsgBox ("Not Found")
> End If
> End Sub
>
>
> I'd like to check if the submitted partnum is already existed in the
> PartNum Table. However, how could I have the Select query executed? With
> OpenRecordset("PartNum") it just return the first record in the PartNum.
> How could fix this?
>
> Thanks.



 
Reply With Quote
 
Vincent Verheul
Guest
Posts: n/a
 
      6th Nov 2009
Hi,

In your code you're creating a new query in the database. You would have to
create it with a parameter and execute it later with a value for that
parameter.

There is a much easier way to do this in VBA without creating a new query in
Access:

Function FindPart(PNum1 As String)
Dim rs As Recordset

Set rs = CurrentDb.OpenRecordset("PartNum", dbOpenSnapshot)

If rs.RecordCount > 0 Then
rs.FindFirst "PartNum='" & PNum1 & "'"
FindPart = Not rs.NoMatch
End If

rs.Close

End Function



"Aldred@office" <aldred> wrote in message
news:(E-Mail Removed)...
> Hi,
> I have a piece of codes like this:
>
> Private Sub Submit_Click()
>
> Dim dbs As Database
> Dim qdf As QueryDef
> Set dbs = CurrentDb()
> Set qdf = dbs.CreateQueryDef(CheckPartNum, "Select PartNum from PartNum
> where PartNum='" & PNum1 & "'")
> Set rst = dbs.OpenRecordset("PartNum") 'PartNum is a table name
>
> If Not rst.EOF Then
> MsgBox ("Found")
> Else
> MsgBox ("Not Found")
> End If
> End Sub
>
>
> I'd like to check if the submitted partnum is already existed in the
> PartNum Table. However, how could I have the Select query executed? With
> OpenRecordset("PartNum") it just return the first record in the PartNum.
> How could fix this?
>
> Thanks.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GoTo Next Record coding smk23 Microsoft Access VBA Modules 1 12th Mar 2008 03:27 PM
New Record Auto ID Coding =?Utf-8?B?QUtwaGlkZWx0?= Microsoft Access Form Coding 1 20th Jun 2007 04:00 AM
Prev/Next Record coding =?Utf-8?B?Z2xuYm56?= Microsoft Access Form Coding 1 10th May 2007 10:47 PM
Problems with coding and coding question!! James Microsoft Access Form Coding 0 23rd Feb 2004 10:34 AM
Re: Record lock coding... TC Microsoft Access VBA Modules 1 26th Sep 2003 10:02 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:41 PM.