Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old November 23rd, 2004, 09:11 AM
ots ots is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 3 ots User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Query answer into a text box in a form

Hi!
This is my first post here. I have a problem with a SQL query in the VB code for a button in a form in Access. Not good language...

Here's what I want to do:
Criteria 1:
I want to get a the highest number(High) from a list. Then add 1 to High and put it in a textbox/field in a form called frmInventory. This form is made from a table called tblIventory

Criteria 2:
High depends on another number(articlenr). articlenr is shown in frmInventory. What I want to do is to get the highest High related to articlenr.

I'm not sure if you understand what I mean. But here's my code and I guess every help is helpfull to me since I'm new to VB and SQL

Code:
    Private Sub LagrePostInv_Click()
    	Dim SQL
    	Dim High As Integer
    	
    	SQL = "SELECT MAX (InventoryNr) AS " & High &" " & _
    	"FROM tblInventory" & _
    	"WHERE tblInventory.articlenr = frmInventory.articlenr"
    	Form_frmInventory.InventoryNr.Value = High
    

Reply With Quote
  #2  
Old November 23rd, 2004, 04:28 PM
Doug G Doug G is offline
Grumpier Old Moderator
ASP Free God 11th Plane (10000 - 10499 posts)
 
Join Date: Sep 2003
Posts: 10,143 Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 21 h 32 m 23 sec
Reputation Power: 180
You haven't done anything with your sql string. You need to execute the sql on a database connection to get a recordset returned with the results of your SELECT. Assuming an active connection con, something like
Code:
SQL = "SELECT MAX (InventoryNr) AS  High " & _
    	"FROM tblInventory" & _
    	"WHERE tblInventory.articlenr = frmInventory.articlenr"
Set RS = con.Execute(SQL)
Form_frmInventory.InventoryNr.Value = RS("High")


The WHERE will return a max number by inventory number, not overall. Leave out the WHERE if you want a max for the whole table.
__________________
======
Doug G
======
I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain

Reply With Quote
  #3  
Old November 24th, 2004, 02:29 AM
ots ots is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 3 ots User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thaks for your help. After some research I found out that I have to execute the sql string. And I guess I have to create a connection to the access database. But how do I do that? This VB code is for a button in a Access form.

Reply With Quote
  #4  
Old November 24th, 2004, 01:13 PM
Lauramc's Avatar
Lauramc Lauramc is offline
SQL Slarentice
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2004
Location: In My Happy Place
Posts: 1,783 Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 15 h 48 m 56 sec
Reputation Power: 1095
Executing SQL String in Access....

If you are using an ADO, you could try something like this:
'general declaration
Dim CN as ADODB.Connection
Dim RS as ADODB.Recordset

Then in your event Button1_Click() for example:
Set CN = CurrentProject.Connection
Set RS = New ADODB.Recordset

RS.Open "SELECT [maxIn] = MAX(InventoryNr) FROM sometable...", CN, adOpenDynamic

If Not (rs.EOF And rs.BOF) Then
Textbox1.Caption = rs!maxIn
End If

RS.Close

Of course you can do this any number of ways, this is just one. I hope it helps! This is the way I do it
if I want a result in a textbox.

Reply With Quote
  #5  
Old November 25th, 2004, 05:37 AM
ots ots is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 3 ots User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks alot.. That was very helpfull. It worked now.

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > Query answer into a text box in a form


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway