|
-
Jun 17th, 2001, 07:33 AM
#1
Thread Starter
Lively Member
Am I right?
I'd like to work with data of a table by VBA code not by MS Access interface. There is another way to do it, than to use this:
Public Sub MySub()
...
DoCmd.Opentable MyTable
...
End Sub
Am I right? Please, advise what code is the best for this?
Best regards,
Oleksandr Valchyshen
A new one in VBA programming
http://www.art-capital.com.ua/?lang=english
-
Jun 17th, 2001, 04:12 PM
#2
PowerPoster
Not quite sure what you mean. VBA is the language of the Access UI. You can access tables via VB only to read/write info.
-
Jun 18th, 2001, 12:42 AM
#3
Thread Starter
Lively Member
I am developing a db in MS Access 2000. Sometimes I use VBA (by writing modules) to solve certain tasks within my db. For example, I need some info from a table, I think that doing this by writing a simple code is much more easier way than creating a query.
Originally posted by chrisjk
Not quite sure what you mean. VBA is the language of the Access UI. You can access tables via VB only to read/write info.
Best regards,
Oleksandr Valchyshen
A new one in VBA programming
http://www.art-capital.com.ua/?lang=english
-
Jun 18th, 2001, 06:39 AM
#4
Fanatic Member
Lets be clear about this. In your first post, it looks like you want to open a table for a user to view and manipulate. if this is the case, then DoCmd.OpenTable is a fairly effective way of doing this in VBA.
In your second post, it seems like you want to only view a certain selection of data. In this case you need a Query.
Is your question "How do I create a query programmatically using VBA?"
-
Jun 18th, 2001, 06:52 AM
#5
Thread Starter
Lively Member
May be I was wrong in my 1st message, but I my question is "How read data from a table using VBA? I don't want to use DoCmd.Opentable or a Query"
May be I still wrong ... I am sorry, because I want to get quick advise without having a MS Access manual.
Originally posted by Gaffer
Lets be clear about this. In your first post, it looks like you want to open a table for a user to view and manipulate. if this is the case, then DoCmd.OpenTable is a fairly effective way of doing this in VBA.
In your second post, it seems like you want to only view a certain selection of data. In this case you need a Query.
Is your question "How do I create a query programmatically using VBA?"
Best regards,
Oleksandr Valchyshen
A new one in VBA programming
http://www.art-capital.com.ua/?lang=english
-
Jun 18th, 2001, 08:06 AM
#6
Fanatic Member
So the final result is going to be a datagrid, in Access, for the user to see data?
-
Jun 18th, 2001, 08:21 AM
#7
Thread Starter
Lively Member
The final result is to find a record by some criterion, then take the value of a field and compare it with field's value from previous record.
Somebody advised me here to do this by SQL query, but I failed, because an error occured while compiling it.
Somebody gave me this code
Recordset.MoveLast
Recordset.MovePrevious 'next to last
Recordset.MoveNext 'last
Originally posted by Gaffer
So the final result is going to be a datagrid, in Access, for the user to see data?
Best regards,
Oleksandr Valchyshen
A new one in VBA programming
http://www.art-capital.com.ua/?lang=english
-
Jun 18th, 2001, 08:39 AM
#8
Fanatic Member
Now we have it! OK, you need a quick tutorial on recordsets. Are you using Access 97?
-
Jun 18th, 2001, 08:45 AM
#9
Thread Starter
Lively Member
Access 2000
Originally posted by Gaffer
Now we have it! OK, you need a quick tutorial on recordsets. Are you using Access 97?
Best regards,
Oleksandr Valchyshen
A new one in VBA programming
http://www.art-capital.com.ua/?lang=english
-
Jun 18th, 2001, 09:11 AM
#10
Fanatic Member
OK, the standard data access tool for 2000 (and everything else for the forseeable future) is ADO.
You need to start a new function, and put this code in:
Code:
Dim rec As Recordset
Set rec = New Recordset
rec.Source = "Select * From Table1;" 'This is where you put your SQL statement to get the recordset
rec.ActiveConnection = Currentproject.connection
rec.Open
This creates a recordset that allows you to step through each line and each field of data. So, if you want to get the value of a field called customer in record one, you use:
Code:
rec.MoveFirst
strCust1 = rec("Customer")
and to get line 2:
Code:
rec.MoveNext
strCust2 = rec("Customer")
Then you can do your string comaprison.
Also, rec.MovePrevious also exists...
-
Jun 18th, 2001, 09:58 AM
#11
Thread Starter
Lively Member
Thank you very much for help! I'll try tonight.
Keep in touch,
Best regards,
Oleksandr Valchyshen
A new one in VBA programming
http://www.art-capital.com.ua/?lang=english
-
Jun 18th, 2001, 10:04 AM
#12
Fanatic Member
heheh, no, i think YOU'D better keep in touch
-
Jun 18th, 2001, 10:08 AM
#13
Thread Starter
Lively Member
You are right!

Originally posted by Gaffer
heheh, no, i think YOU'D better keep in touch
Best regards,
Oleksandr Valchyshen
A new one in VBA programming
http://www.art-capital.com.ua/?lang=english
-
Jun 18th, 2001, 10:16 AM
#14
Fanatic Member
Originally posted by valchyshen
You are right!

let me know how you get on....
-
Jun 21st, 2001, 05:03 AM
#15
Thread Starter
Lively Member
Hi! 
You know it works very well! This is my code:
Public Sub test()
Dim rec As Recordset
Set rec = New Recordset
rec.Source = "SELECT TOP 2 * FROM pfts_index ORDER BY Date DESC;"
rec.ActiveConnection = CurrentProject.Connection
rec.Open
rec.MoveFirst
CurIndex = rec("Index")
rec.MoveNext
PrvIndex = rec("Index")
DailyIndexChange = ((CurIndex - PrvIndex) / CurIndex) * 100
End Sub
But I'd like to put a date parameter into SQL query, what I have to do, please?
Originally posted by Gaffer
let me know how you get on....
Best regards,
Oleksandr Valchyshen
A new one in VBA programming
http://www.art-capital.com.ua/?lang=english
-
Jun 21st, 2001, 05:34 AM
#16
Fanatic Member
looks good. To get a parameter in, first set a variable:
Code:
Dim sMyDate as String
Populate the varianle
Code:
sMyDate = "21/02/98"
Now, here, I always format to US standards, regardless of the scenario
Code:
sMyDate = Format$(sMyDate,"mm/dd/yyyy")
Then the SQL becomes:
Code:
rec.Source = "SELECT TOP 2 * FROM pfts_index WHERE Date=#" & sMyDate & "# ORDER BY Date DESC;"
Oh, by the way, I would avoid using Date as a table column name - Date is a reserved word and could be confused with
Date() function. Don't worry too much if you're too far down the road to change it...
-
Jun 23rd, 2001, 01:42 AM
#17
Thread Starter
Lively Member
There is a problem. In the code:
rec.Source = "SELECT top 2 * FROM pfts_index WHERE Date<=# & DateStr & # ORDER BY Date DESC"
the parameter DateStr, which recieved by InputBox function, has following format "21.06.2001". And the code does not work. If I put "21/06/2001" instead of DateStr, it works well. I think the matter is in the date separators ("." and "/"). What I have to do, please?
Best regards,
Oleksandr Valchyshen
A new one in VBA programming
http://www.art-capital.com.ua/?lang=english
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
|