Advertisement

05.09.2008 at 02:40PM PDT, ID: 23390896 | Points: 125
[x]
Attachment Details
Using Variables to refer to column name, for example: frmSubform.Form![strMonthName(3)] or another workaround.
I have a form with a sub form. On the open event, it opens a textfile to get a date string. A SQL statement then uses this string to make 6 variable column names ([Apr], [May], [Jun], [Jul], etc., depending on time of year). This all works fine. The problem is with trying to set properties for these new columns, since there's no easy way to refer to them. I'm trying to set column width so that everything fits in the sub form window with no scrolling, but can't figure out how to refer to them. I tried doing it through tabledefs, but I couldn't find a way. I wish we had something like DoCmd.RunVBA
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
Private Sub Form_Open(Cancel As Integer)
Dim strUser As String
Dim strSQL As String
Dim MonthFile As String, strDate As String, intMonth As Integer
Dim MonthVal(6) As Integer, y As Integer
Dim MonthString(6) As String
Dim qd As QueryDef, db As Database
Dim rs As ADODB.Recordset
 
Dim sqlDel As String
 
frmSubform.Form.RecordSource = ""
sqlDel = "DROP TABLE tblProjectionsTemp"
DoCmd.RunSQL sqlDel
 
Set db = CurrentDb
Set qd = db.QueryDefs("qryProjections")
 
MonthFile = "H:\Database\Monthstart.txt"
 
Open MonthFile For Input As #1
Input #1, strDate
Close #1
 
intMonth = Month(DateValue(strDate) - 10)
 
For y = 0 To 6 Step 1
  MonthVal(y) = intMonth + y
  MonthString(y) = Left(MonthName(MonthVal(y)), 3)
Next y
 
strUser = Environ("Username")
Me.Caption = strUser & ": Six Month Projections"
 
qd.Sql = "SELECT tblProjections.[Job Number], tblFullJoblist.[Job Name], tblProjections.[Last Month] AS " & MonthString(0) & ", tblProjections.[Current Month] AS " & MonthString(1) & ", tblProjections.Month2 AS " & MonthString(2) & ", tblProjections.Month3 AS " & MonthString(3) & ", tblProjections.Month4 AS " & MonthString(4) & ", tblProjections.Month5 AS " & MonthString(5) & ", tblProjections.Month6 AS " & MonthString(6) & ", tblProjections.employee INTO tblProjectionsTemp " _
         & "FROM tblProjections INNER JOIN tblFullJoblist ON tblProjections.[Job Number] = tblFullJoblist.[Job Number] " _
         & "WHERE (((tblProjections.employee)='" & strUser & "'));"
 
qd.Execute
 
frmSubform.SourceObject = "Table.tblProjectionsTemp"
frmSubform.Locked = False
 
With frmSubform.Form
  .AllowAdditions = False
  .AllowDeletions = True
  ![Job Name].Locked = True
  ![Job Name].ColumnWidth = 3.248 * 1440
  ![Job Number].Locked = True
End With
 
 
End Sub
Start your free trial to view this solution
Question Stats
Zone: Microsoft
Question Asked By: lhjunius
Question Asked On: 05.09.2008
Participating Experts: 2
Points: 125
Views: 0
Translate:
Loading Advertisement...
05.09.2008 at 03:41PM PDT, ID: 21536839

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 04:02PM PDT, ID: 21536924

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 04:06PM PDT, ID: 21536940

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 04:31PM PDT, ID: 21537084

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 04:54PM PDT, ID: 21537237

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 05:24PM PDT, ID: 21537325

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 05:25PM PDT, ID: 21537329

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 07:34AM PDT, ID: 21547324

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 08:01AM PDT, ID: 21547546

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 09:10AM PDT, ID: 21548171

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 09:12AM PDT, ID: 21548191

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 10:08AM PDT, ID: 21548696

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 02:08PM PDT, ID: 21550535

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.09.2008 at 03:41PM PDT, ID: 21536839
Where are you trying to reference the column names? An example would help...
 
05.09.2008 at 04:02PM PDT, ID: 21536924
Sorry, I took out the code that wasn't working. Bad idea for this purpose

With frmSubform.Form
  .AllowAdditions = False
  .AllowDeletions = True
  ![Job Name].Locked = True
  ![Job Name].ColumnWidth = 3.248 * 1440
  For Y = 0 to 6
    ![MonthString(0)].columnWidth = 600
  Next Y
End With

This would be an example of what I'm trying to do. Obviously this doesn't work. I don't have to do it by this method, I'm just having a hard time figuring out how to set a columns formatting in VB.
 
05.09.2008 at 04:06PM PDT, ID: 21536940
Change this:

![MonthString(0)].columnWidth = 600

to this:

![MonthString(Y)].columnWidth = 600
 
05.09.2008 at 04:31PM PDT, ID: 21537084
I'm sorry, that was just a typo when I was writing that code for this site. Just trying to demonstrate the concept. Access isn't accepting variables at all for column names, it looks for a column literally named "Monthstring" and crashes when it doesn't find it. I'm just trying to find another way to figure this out.
 
05.09.2008 at 04:54PM PDT, ID: 21537237
"Monthstring"? Then perhaps this will help:
1:
2:
3:
qd.Sql = "SELECT tblProjections.[Job Number], tblFullJoblist.[Job Name], tblProjections.[Last Month] AS [" & MonthString(0) & "], tblProjections.[Current Month] AS [" & MonthString(1) & "], tblProjections.Month2 AS [" & MonthString(2) & "], tblProjections.Month3 AS [" & MonthString(3) & "], tblProjections.Month4 AS [" & MonthString(4) & "], tblProjections.Month5 AS [" & MonthString(5) & "], tblProjections.Month6 AS [" & MonthString(6) & "], tblProjections.employee INTO tblProjectionsTemp " _
         & "FROM tblProjections INNER JOIN tblFullJoblist ON tblProjections.[Job Number] = tblFullJoblist.[Job Number] " _
         & "WHERE (((tblProjections.employee)='" & strUser & "'));"
Open in New Window
 
05.09.2008 at 05:24PM PDT, ID: 21537325

Rank: Genius

Why not find a width that fits all the combos and use that for all of them.
 
05.09.2008 at 05:25PM PDT, ID: 21537329

Rank: Genius

The next thing you'll discover is that some of the 'values' wont fit the column width, and on and on ....
 
05.12.2008 at 07:34AM PDT, ID: 21547324
"Why not find a width that fits all the combos and use that for all of them."

I'm not sure of the method to do this. I wouldn't mind setting every column in the form to a set size.
MonthString(y) = Left(MonthName(MonthVal(y)), 3) ensures that all month names are changed to 3 letters, so the column could be fairly small and still fit them all. Job Name and Job Number need to be larger on the subform, but if I could set all columns to a smaller width (that doesn't fill the subform completely) and then change those two after the fact, to make the whole thing fit exactly (without no scroll bar), that would work too.
 
05.12.2008 at 08:01AM PDT, ID: 21547546
*without a scroll bar

I should point out that the table is being created with the names I need, the data's all there. It's just a formatting issue I'm trying to figure out.

 
05.12.2008 at 09:10AM PDT, ID: 21548171
I finally broke down and used a hacky solution. It works, but it ain't pretty.
If anyone can provide a more elegant way of doing this, I'll award you the points.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
  On Error Resume Next
    ![Jan].ColumnWidth = 0.645 * 1440
    ![Feb].ColumnWidth = 0.645 * 1440
    ![Mar].ColumnWidth = 0.645 * 1440
    ![Apr].ColumnWidth = 0.645 * 1440
    ![May].ColumnWidth = 0.645 * 1440
    ![Jun].ColumnWidth = 0.645 * 1440
    ![Jul].ColumnWidth = 0.645 * 1440
    ![Aug].ColumnWidth = 0.645 * 1440
    ![Sep].ColumnWidth = 0.645 * 1440
    ![Oct].ColumnWidth = 0.645 * 1440
    ![Nov].ColumnWidth = 0.645 * 1440
    ![Dec].ColumnWidth = 0.645 * 1440
   On Error GoTo 0
Open in New Window
 
05.12.2008 at 09:12AM PDT, ID: 21548191
Correct me if I am wrong, but isn't this a different question than the one posted originally?

If so, award poinks (or not) and ask this in a new thread.
 
05.12.2008 at 10:08AM PDT, ID: 21548696
Same question. It's kind of a confusing problem to explain. I know it's easier for me to read code that I wrote and get  the concepts than to look at somebody else's.
  I wasn't having problems changing the query, the column titles were being renamed exactly like I wanted them to. The problem was when viewing it in a subform, any new column name (the last one, which is the new month that's being introduced [6 Months from the current one]) would be the default width of 11.75, which would make the subform larger than the window I had given it, meaning users would have to use the scrollbar.
  It was purely a formatting issue, trying to find a way to use the .ColumnWidth method on a column with a name that's subject to change. 'frmSubform.form![VariableName].columnWidth' looks for a column literally named "variableName" as opposed to the string that variable contains.  
  I found a working solution for it, but I'm trying to develop good coding habits, and avoid things like "on error resume next". This isn't really a critical problem any more, but I don't want to take my ball and go home when people have devoted time to figuring this out.
  For my own education, I would like to see a better solution to this problem, so I'm leaving the thread open.
 
05.12.2008 at 02:08PM PDT, ID: 21550535

Rank: Genius

In place of:

![Jan].ColumnWidth = 0.645 * 1440
etc.

For i=n to m ' n is the Jan control number, m is the Dec control number - hopefully all sequential
  !Controls(i).ColumnWidth = 0.645 * 1440
Next i

don't forget to dim i, n, m
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628