Isis said:
I am currently using something like
Me.Controls(strFieldName).SetFocus
strT = Me.Controls(strFieldName).Text
The above is wrong. You DO NOT WANT to use setfocue for JUST reading a
controls value. When you do the above, all kinds of things can happen.....
Simply use
me.Fieldname
or
me.Fieldname.value (value is the default property..so, it is
optional)
the .text property is a SPECIAL AND RARE used property of the control that
is ONLY used for while the control has the focus. When you use the .value
property, the you do NOT have to change the focus.
msgbox "last name field on form is " & me.LastName
msgbox "first name field on forms is " & me.FirstName
Note how the above we do not change the focus. So, drop that use of focus,
it is extra code. It is code that forces the cursor to change focus JUST to
read, or set a silly value....no need to do this....
(note: in VB6, they always used the .text, and in ms-access, the equivalnat
is the .value. We have BOTH propeites abvialing..but the .text is speical in
ms-access....and, if you are comming from a VB envirment...use .value, not
..text).
Is there a way of getting at and manipulating field data from various
Tables from within my VB code in Access ? Something like the following;
CUSID = 'JONES1'
Get(CustomerTable)
strPCode = CUSPOSTCODE
Yes, there is a number of way to grab data from tables. Which approach you
use will depend on what you need to do..
The most easy function to use is called dlookup()
The dlookup() function is as follows
dlookup("field name", "table name", "where condition"
So, to grab a value of lastname of of a customer reocrd with a id of 123, we
would use
dim strLastName as string
strLastName = dlookup("LastName","tblCustomers","id = 123")
or, we could prmpt for the id number
dim strLastName as string
dim strID as string
strID = inputbox("What customer to see name of")
strLastName = dlookup("LastName","tblCustomers","id = " & strID)
However, if we needed the first name, and phone number, then It would be
rather expensive to query the database over and over for each field. So, if
you need several values, the we would use a reocrdset. Note that reocrdets
are also used for processing MORE THEN one record of data in code.
dim rstCustomer as dao.recordset
dim strSql as string
strSql = "select * from tblCustomer where id = 123"
set rstCustomer = currentdb.openreocrdset(strSql)
At this point, we can use all values from the reocrd
rstCustomer!LastName
rstCustomer!Firstname
etc. etc....
or, to process many reocrds...
strSql = "select * from tblCustomer where City = 'Edmonton' "
set rstCustomer = currentdb.openreocrdset(strSql)
do while rstCustomer.EOF = false
msgbox "about to process customer = " & rstCustomer!CompanyName
' your processing code goes here....
rstCustomer.movenext
loop
rstCustomer.Close
set rstCustomer = nothing
I would suggest you get yourself a book that has some coding examples....