Storing databse information in text box

xxdcmast
04-16-2004, 06:28 AM
Assuming that I have a working connection to a database is there a way that by using a SQL select statement that I can take the result from the database and store it in a textbo for further use. The result will only be one record from one column.

If this is possible can somebody please show me a sample of the code that will perform this action. Thanks Alot

Syko10-96
04-16-2004, 06:41 AM
something akin to:

textbox1.text = rs.Fields(x).value

xxdcmast
04-16-2004, 06:52 AM
something akin to:

textbox1.text = rs.Fields(x).value

Can this be used with a select statement like

textbox1.text = Select COLUMN_NAME From TABLE Where ROW_NUM = 5

Syko10-96
04-16-2004, 06:57 AM
no - after you create your recordset (I typically use ADO...) just replace "x" with "COLUMN_NAME"

e.g.:

' assuming your connection is already established
dim rst as ADODB.Recordset
set rst = cnn.Execute("Select COLUMN_NAME From TABLE Where ROW_NUM = 5")
if not rst.eof then
textbox1.text = rst.Fields("COLUMN_NAME").value
else
' record was not found
end if
'close recordset, connection, etc...

msmeth
04-16-2004, 07:00 AM
no - after you create your recordset (I typically use ADO...) just replace "x" with "COLUMN_NAME"

e.g.:

' assuming your connection is already established
dim rst as ADODB.Recordset
set rst = cnn.Execute("Select COLUMN_NAME From TABLE Where ROW_NUM = 5")
if not rst.eof then
textbox1.text = rst.Fields("COLUMN_NAME").value
else
' record was not found
end if
'close recordset, connection, etc...


I tend to use rst![Column Name] to access the field from the returned recordset and am just wondering what's the difference between using that and this rst.Fields("COLUMN_NAME").value method? I see people using the latter quite a bit, but I've never used it and am just wondering...is it better or something?

MKoslof
04-16-2004, 07:04 AM
Be very careful here :). In the given situation this will work, however, it is NOT good practice to use the Cnn.execute method to open a recordset. This by default creates a forward only cursor. If you want to do additional modifications to this recordset after opening (looping until EOF, recordcount, comparisons, etc). You should use the recordset object's .Open method




Dim rs As ADODB.recordset
Dim sSQL as string

sSQL = "SELECT Column_Name FROM Table WHERE Row_Num = 5"
Set rs = New ADODB.recordset

'where cn is your ADODB connection

rs.Open sSQL, cn, adOpenKeyset, adLockOptimistic, adCmdText

If NOT rs.EOF = 0 Then
msgbox "No records found"
Exit sub
End If

text1.text = rs.Fields("Column_Name").value

rs.close
Set rs = Nothing

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum