Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Word, PowerPoint, Outlook, and Other Office Products > Counting records and setting value in table


Reply
 
Thread Tools Display Modes
  #1  
Old 01-29-2004, 06:18 AM
VB-wanna-b VB-wanna-b is offline
Centurion
 
Join Date: Jul 2003
Posts: 169
Default Counting records and setting value in table


I defined a string value (dim as string) that contains an sql string that filters out a specifik set of records.

code:


Dim UsrStr As String
Dim sSql As String
Dim comp As Long
UsrStr = Me.MyFieldFilter 'the filter value that is used as identifier to
'what records should be counted
'dim comp as DAO.RecordSet


sSql = "SELECT * FROM TBL_1 WHERE TBL_1.A1r= " & UsrStr & ""
'Set comp = CurrentDb.OpenRecordset(sSql)
'Me.MyField = comp.RecordCount (sSql)
'comp = DCount("*", "TBL_1", ["A1"] = " & UsrStr & ")
comp = DCount("[A1]", sSql)
Me.MyField= comp

as is shows I´ve tried a few ways...cant get it to work..
hope you can help me out..

Last edited by VB-wanna-b; 01-29-2004 at 06:23 AM.
Reply With Quote
  #2  
Old 01-29-2004, 07:26 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Do something like this:

Code:
Dim sSQL as string Dim myText as string 'your standard SQL with the where condition, but use the COUNT 'method to get the number of matching records sSQL = "SELECT COUNT(*) as total_Count FROM myTable Where field1 = '" & text1.text & "'" 'open the sSQL recordset and then grab the value of the total_count field myText = rs.fields("total_count").value
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #3  
Old 01-29-2004, 03:08 PM
VB-wanna-b VB-wanna-b is offline
Centurion
 
Join Date: Jul 2003
Posts: 169
Default

when I debug it, I get total_Count out as empty...but in fact it should say "3"

made an msgbox total_count
...blank

and I get en "Object requiered" error when I run it

but running the sql in query mode., gives me "3" as the value og total_Count
Reply With Quote
  #4  
Old 01-29-2004, 03:12 PM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

post your code...did you do what I said?

You can not do msgbox total_count. Total count is now an aliased field created by the sql

msgbox rs.fields("total_Count").value
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #5  
Old 01-30-2004, 01:22 AM
VB-wanna-b VB-wanna-b is offline
Centurion
 
Join Date: Jul 2003
Posts: 169
Default

Dim UsrStr As String
Dim sSql As String
Dim comp As String
UsrStr = Me.AfdNr

'the sql is one long line - not split up.

sSql = "SELECT COUNT (*) as total_Count FROM TBL_bruger WHERE AfdNr= " & UsrStr & "" 'when run in querymode it gives me "3" which is correct


'Set comp = CurrentDb.OpenRecordset(sSql)
'MsgBox comp
'comp = DCount("*", "TBL_bruger", ["AfdNr"] = " & UsrStr & ")
'comp = DCount("[AfdNr]", sSql)
'Me.Bruger_antal = comp
'comp = rs.Fields("total_Count").Value
'MsgBox "& total_Count &"
MsgBox rs.Fields("total_Count").Value 'gets me "Object required"

comp = rs.Fields("total_Count").Value

Me.Bruger_antal = comp
compDoCmd.SetWarnings True
'MsgBox (comp)
'Me.Bruger_antal = comp.RecordCount
Reply With Quote
  #6  
Old 01-30-2004, 06:21 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

yes, the SELECT statement will work, as you described, look at what I am doing here.

comp = rs.Fields("total_Count").Value


this reason this doesn't work is, you have NOT declared rs (or the recordset) in your code. I am saying here the variable comp is equal to the RECORDSET field "total_count". You have not declare the rs as recordset, and you have not opened it.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #7  
Old 01-30-2004, 07:34 AM
VB-wanna-b VB-wanna-b is offline
Centurion
 
Join Date: Jul 2003
Posts: 169
Default

woops..slipped my mind... I changed it a bit, but I now get into an infinate loop of such..."Action can not be completed while object is closed"
I do close the recordset again... but I AM supposed to do that..cant leave open at all time..

Private Sub Bruger_antal_GotFocus()
On Error GoTo Bruger_antal_GotFocus_Err


Dim UsrStr As String
Dim sSql As String
Dim comp As String
Dim rs As ADODB.Recordset
UsrStr = Me.AfdNr

Set rs = New ADODB.Recordset


rs.Open "SELECT COUNT (*) as total_Count FROM TBL_bruger WHERE AfdNr= " & UsrStr & ""
'Set comp = CurrentDb.OpenRecordset(sSql)
'MsgBox comp
'comp = DCount("*", "TBL_bruger", ["AfdNr"] = " & UsrStr & ")
'comp = DCount("[AfdNr]", sSql)
'Me.Bruger_antal = comp
'comp = rs.Fields("total_Count").Value
'MsgBox "& total_Count &"
MsgBox rs.Fields("total_Count").Value

comp = rs.Fields("total_Count").Value

Me.Bruger_antal = comp
compDoCmd.SetWarnings True
'MsgBox (comp)
'Me.Bruger_antal = comp.RecordCount
Bruger_antal_GotFocus_exit:
rs.Close
DoCmd.SetWarnings True
Exit Sub

Bruger_antal_GotFocus_Err:
MsgBox Err.Description
Resume Bruger_antal_GotFocus_exit

End Sub
Reply With Quote
  #8  
Old 01-30-2004, 07:39 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

All you should be doing is:

Open your recordset and run your query
get the total_Count value
Close the recordset, if that is what you need to do.

Then everytime this "procedure" or "event" is run, the events occur again without issue. If you open a recordset, do an event, and then close it..there is no reason to get an error regarding it being closed. You are opening and closing the valid recordset with every event trigger...make sure your recordset is being opened and closed properly.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #9  
Old 01-30-2004, 07:48 AM
VB-wanna-b VB-wanna-b is offline
Centurion
 
Join Date: Jul 2003
Posts: 169
Default

Dim UsrStr As String
Dim sSql As String
Dim comp As String
Dim rs As ADODB.Recordset
UsrStr = Me.AfdNr

Set rs = New ADODB.Recordset '1 Define recordset

rs.Open "SELECT COUNT (*) as total_Count FROM TBL_bruger WHERE AfdNr= " & UsrStr & "" 'Open recordset

comp = rs.Fields("total_Count").Value 'getting value of total_Count

Me.Bruger_antal = comp 'Assigning value to a field (not an unbound)
rs.Close 'Close connection
Bruger_antal_GotFocus_exit:

DoCmd.SetWarnings True
Exit Sub

Bruger_antal_GotFocus_Err:
MsgBox Err.Description
Resume Bruger_antal_GotFocus_exit

End Sub


If I follow what you wrote, then this should work....
But it cannot run on a illegal connection or on a closed object it says...
what Im I missing
Reply With Quote
  #10  
Old 01-30-2004, 07:55 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

First of all where is this event coded? Do not use the Got_focus event. Use the Changed event or something else. As an initial test, just put it in a command button for now.


Code:
Dim sSql As String Dim rs As ADODB.Recordset Dim cn as ADODB.connection Dim UsrStr as string Dim comp as string UsrStr = Me.AfdNr sSQL = "SELECT COUNT (*) as total_Count FROM TBL_bruger WHERE AfdNr= " & UsrStr & "" Set cn = New ADODB.connection Set rs = New ADODB.Recordset cn.connectionstring = "your ADO Connection string" cn.open 'open the ADO connection 'open your recordset rs.Open sSQL, cn, adOpenKeyset, adlockOptimistic, adcmdtext 'getting value of total_Count comp = rs.Fields("total_Count").Value 'stop here for now msgbox comp rs.close cn.close Set rs = Nothing Set cn = Nothing End Sub
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #11  
Old 01-30-2004, 05:03 PM
VB-wanna-b VB-wanna-b is offline
Centurion
 
Join Date: Jul 2003
Posts: 169
Default

I put it in a click event procedure...now... :-)
looks like this.. the Error I get now is
"[Microsoft][ODBC Driver Manager] Datasourcename not found, and no default driver selected.



Private Sub Bruger_antal_Click()
On Error GoTo Bruger_antal_Click_Err

Dim UsrStr As String
Dim sSql As String
Dim comp As Integer

Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection


UsrStr = Me.AfdNr


sSql = "SELECT COUNT (*) as total_Count FROM TBL_bruger WHERE AfdNr= " & UsrStr & ""
Set rs = New ADODB.Recordset
Set cn = New ADOB.Connection


'Establishing ADOB connection

cn.ConnectionString = "CurrentProject.Connection "
cn.Open

'Open recordset based on sSql

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

'retrieve value of total_Count which is number of records with usrStr as key value

comp = rs.Fields("total_Count").Value

MsgBox comp


rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Access: Help with finding records... meteo Word, PowerPoint, Outlook, and Other Office Products 5 11-20-2003 01:51 PM
Restricting the display of records vikassethi Database and Reporting 1 09-01-2003 07:30 AM
HHHHHEEEEEELLLLPPPPP:::::::multiple records in a form MatthijsBonte Database and Reporting 1 07-30-2003 01:52 AM
Recordset Update - Related Records Problem punkrawk Database and Reporting 6 02-24-2003 02:22 PM
Setting Access database table properties. ReinierEds Database and Reporting 2 07-11-2002 12:55 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->