Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

An easy VB MS Access 2000 question

I have a single table (Employees) with 3 rows: Name, Age and Salary

I have one form with a command control

What code do I have to write in order to perform a simple search (salary>1000)
and print all the data relevant.
I've tried to write a code, but I'm having a problem with the CurrentDB.OpenRecordset and with the search function.

Please try to help me.
Thanks
0
alexel
Asked:
alexel
1 Solution
 
undagroundCommented:
Try this code

Dim salary(1 to 3) as integer, x as integer

Private Sub Command1_Click()
For x=1 to 3
If salary(x)>1000 then
Picture1.print salary(x)
End if
Next x
End Sub

Hope it helps!
-Mike
0
 
wide_awakeCommented:
dim rst as DAO.Recordset
set rst = CurrentDB.OpenRecorset "Select * from Employees where [Salary] > 1000"

do while !rst.EOF
  ' print the data from the current record
  Debug.print(rst("Name"))
  Debug.print(rst("Age"))
  Debug.print(rst("Salary"))
  rst.MoveNext
loop


That should do it...

-Mark
0
 
alexelAuthor Commented:
Mark

Thanks, but I'm getting a compile error on the:
Dim rst as DAO.Recordset

which is User Defined type not defined.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
arbertCommented:
Do you have a reference to the DAO object in your project????

Brett
0
 
wide_awakeCommented:
You have to add a reference for DAO - To do that in Access:

Go to the VB screen, then go to Tools -> References

In there, make sure "Microsoft DAO 3.6 Object Library" is selected.  Then it should recognize DAO.Recordset as a valid data type.


-Mark.
0
 
alexelAuthor Commented:
I've added the reference but now i'm getting a syntax error on the Select function
0
 
wide_awakeCommented:
If you just copy & pasted, I made a typo above... it should be

set rst = CurrentDB.OpenRecordset "Select * from Employees where [Salary] > 1000"

but I entered
> set rst = CurrentDB.OpenRecorset "Select * from Employees where [Salary] > 1000"

without a "d" in OpenRecordset

Ooops.

-Mark
0
 
alexelAuthor Commented:
I've noticed the typo and I've entered the code correctly, but I'm still getting a syntax error.

Do I have to perform additional references or to define something else?

Thanks
Alex
0
 
wide_awakeCommented:
What's the exact error you're getting?
0
 
alexelAuthor Commented:
After I'm clicking the command control, i'm getting a "compile error:syntax error" when the Select function is highlighted.
0
 
alexelAuthor Commented:
After I'm clicking the command control, i'm getting a "compile error:syntax error" when the Select function is highlighted.
0
 
wide_awakeCommented:
What's the exact error you're getting?
0
 
wide_awakeCommented:
Try this:

set rst = CurrentDB.OpenRecordset "Select * from [Employees]"


If that doesn't work, try doing this:

Dim strSQL as String
strSQL = "Select * from [Employees]"
set rst = CurrentDB.OpenRecordset strSQL

If that doesn't work, then something weird is going on.

-Mark.
0
 
alexelAuthor Commented:
Something weird is going on for sure.
The first option didn't work, as for the second it gave the syntax error on the:
set rst = CurrentDB.OpenRecordset strSQL

It seems like it doesn't recognize the CurrentDb parameter.

I'm increasing the points as this problem as not simple as I thought.
0
 
wide_awakeCommented:
if you type "CurrentDb." in the VB window, does it pop up a context menu with all the methods/attributes of CurrentDb?
0
 
wide_awakeCommented:
Oh - one other thing you might need to do is put the SQL string in parentheses.

set rst = CurrentDB.OpenRecordset(strSQL)

0
 
wide_awakeCommented:
Oh - one other thing you might need to do is put the SQL string in parentheses.

set rst = CurrentDB.OpenRecordset(strSQL)

0
 
wide_awakeCommented:
Heh, it's pretty easy to submit the same comment multiple times by accident eh?  :)
0
 
kiranghagCommented:
remove [] from the query
0
 
wide_awakeCommented:
any luck?
0
 
alexelAuthor Commented:
Hey Mark

Sorry for the delay.
Anyway, it still doesn't work, but I'll give you the points for youre help

Thanks
Alex
0
 
wide_awakeCommented:
If you type "CurrentDb." does it come up with a context menu?  

if not, it's probably a missing reference.

If you go to the VB Window, and go to Tools -> References, which libs have checks beside them?

-Mark.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now