Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 1st, 2006, 05:48 AM
Authorized User
 
Join Date: May 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Mapping of Data From Various Columns

I have an employee with an NI Code of A

NI Contribution thresholds are in the format:

A1a A1b A1c A1d

1000 100 10.00 50.00

However there are many NI Codes and many columns of thresholds for each Code. I am trying to write a query to say:

If NI Code = x then give me values from columns x1a x1b x1c x1d etc

Can anyone help?

Thanks


 
Old June 1st, 2006, 08:44 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Hmmm... I am not sure if this will help, but it could work.

Code:
Dim NICode as string
Dim rs as ADODB.Recordset
Dim cn as ADODB.Recordset
Dim strSQL as String

set cn = Application.CurrentProject.Connection
set rs = new ADODB.Recordset
NICode = "A"

strSQL = "SELECT " & NICode & "1a, " & NICode & "1b, " & NICode & "1c, " & NICode & "1d FROM theTableName"

rs.Open strSQL, cn

If Not(rs.EOF and rs.BOF) Then
   MsgBox rs.Fields(NICode & "1a")
   MsgBox rs.Fields(NICode & "1b")
   MsgBox rs.Fields(NICode & "1c")
   MsgBox rs.Fields(NICode & "1d")
End If

rs.Close
cn.Close

Set rs=Nothing
Set cn=Nothing
Hope that helps

Mike



Mike
EchoVue.com
 
Old June 1st, 2006, 09:53 AM
Authorized User
 
Join Date: May 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How can I run this from a select query?

 
Old June 1st, 2006, 10:05 AM
Authorized User
 
Join Date: May 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

When running the code I receive the error: Compile Error: Invalid Outside Procedure

 
Old June 1st, 2006, 10:15 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Are you very familiar with VBA? If not, it's not too difficult and we could probably get through this.

What you probably want is a form with a text box (we'll call it txtLetter) and a button (we'll call this cmdRun)

Next pull up the properties for the button and go to the events tab. Choose On Click, and then hit the button with ... Choose the bottom option - it says something about code.

You should have the start and end of a Sub Procedure. Paste the code from above between those two line.
Next, change NICode="A" to NICode = txtLetter

Save it, run your form and you should get the 4 values popping up in Message boxes after you choose a number and click the button.

Mike

Mike
EchoVue.com
 
Old June 1st, 2006, 10:46 AM
Authorized User
 
Join Date: May 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If I run from form or as a macro I receive the error:

Compile Error: Invalid Outside Procedure


 
Old June 1st, 2006, 10:49 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Usually you only get that error if your code is not enclosed by Sub and End Sub statements.

You shoul have...

Private Sub cmdRun_Click()

Code here

End Sub

The only thing that should be outside of that is any statement starting with Option ...

Mike
EchoVue.com
 
Old June 1st, 2006, 10:53 AM
Authorized User
 
Join Date: May 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is the format of my code:

Private Sub txtLetter_Click()
Dim NICode As String
Dim rs As ADODB.Recordset
Dim cn As ADODB.Recordset
Dim strSQL As String

Set cn = Application.CurrentProject.Connection
Set rs = New ADODB.Recordset
NICode = "txtLetter"

strSQL = "SELECT " & NICode & "1a, " & NICode & "1b, " & NICode & "1c, " & NICode & "1d FROM WorkPlace NI Breakdown"

rs.Open strSQL, cn

If Not (rs.EOF And rs.BOF) Then
   MsgBox rs.Fields(NICode & "1a")
   MsgBox rs.Fields(NICode & "1b")
   MsgBox rs.Fields(NICode & "1c")
   MsgBox rs.Fields(NICode & "1d")
End If

rs.Close
cn.Close

Set rs = Nothing
Set cn = Nothing

End Sub

When prompted with this error the word Set in row: Set cn = Application.CurrentProject.Connection is highlighted

 
Old June 1st, 2006, 10:59 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

My bad!! Change

Dim cn As ADODB.Recordset

to

Dim cn as ADODB.Connection

Sorry!!!

Mike
EchoVue.com
 
Old June 1st, 2006, 11:13 AM
Authorized User
 
Join Date: May 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hmmm still receiving same error






Similar Threads
Thread Thread Starter Forum Replies Last Post
Displaying data in two columns peterh ASP.NET 2.0 Basics 11 January 29th, 2008 11:08 AM
Mapping columns and then copying data Roshanjoshi2001 Excel VBA 0 December 10th, 2007 11:37 AM
data selection and mapping with dropdownlist gagansharma7 ASP.NET 1.0 and 1.1 Basics 0 June 27th, 2006 11:41 AM
Data Report: Columns Against Rows bemular VB How-To 0 January 6th, 2005 12:52 AM
Getting the data in columns - like Pivot Table merguvan Access VBA 4 February 29th, 2004 02:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.