Hello everyone, first of all allow me to state i am by no means an advanced Access / VB user but consider myself competent to a certain extent  :banghead:
 :banghead:
One of my roles at work is to main and develop an access database that was written by a few of my predecessors. It is used to create production paperwork, order materials, produce delivery notes & invoices etc.
One report that is particularly useful to some of the guys here is a 20 Week Production Plan, which gives them a sum of products and the value Per Job, these are sorted into Delivery Week Number.
The report is driven using a Crosstab Query, and open ReportOpen a short VB function renames and corrects the Label Headings and Datasource of the appropriate fields. The report works perfectly, no issues at all.
We then decided it might be easier and offer more functionality to migrate a copy of this report to a Datagrid Form, this way we can apply some nice Data Validation, provide system navigation through double clicking fields etc.
I thought this should have been relatively simple, however i cant seem to get it to work properly.
When viewing the Query it works as normal, the problem seems to come when trying to display it on the form correctly.
Problems noted so far:
- Query should start on the current week number, and then to 20+ Weeks, in the query this is the case, on the form it misses your current week and starts at the next?
- The For Next Loop which renames the labels and assigns control source appears to stop counting correctly at some point, im not sure if this occurs if there is no data for a particular Week Number, this was not a problem on the Report Version.
- Week Numbers are Duplicated.
Below is the code that controls the fields:
	
	
	
		
I have some screenshots of the Column Naming misbehavior.
From the Query:
https://ibb.co/1GKtHb1
Same Data in the Form:
https://ibb.co/CV0LpY9
Notice the duplicated Field Names & Mixed Order.
I get the concept of how this is supposed to be working but im not convinced about the column counting and field counting - if this is most effective way of achieving the goal. Im also not sure why it works fine on a report and not a form.
Any help would be greatly, greatly appreciated.
Thanks
Alex
  :banghead:
 :banghead:One of my roles at work is to main and develop an access database that was written by a few of my predecessors. It is used to create production paperwork, order materials, produce delivery notes & invoices etc.
One report that is particularly useful to some of the guys here is a 20 Week Production Plan, which gives them a sum of products and the value Per Job, these are sorted into Delivery Week Number.
The report is driven using a Crosstab Query, and open ReportOpen a short VB function renames and corrects the Label Headings and Datasource of the appropriate fields. The report works perfectly, no issues at all.
We then decided it might be easier and offer more functionality to migrate a copy of this report to a Datagrid Form, this way we can apply some nice Data Validation, provide system navigation through double clicking fields etc.
I thought this should have been relatively simple, however i cant seem to get it to work properly.
When viewing the Query it works as normal, the problem seems to come when trying to display it on the form correctly.
Problems noted so far:
- Query should start on the current week number, and then to 20+ Weeks, in the query this is the case, on the form it misses your current week and starts at the next?
- The For Next Loop which renames the labels and assigns control source appears to stop counting correctly at some point, im not sure if this occurs if there is no data for a particular Week Number, this was not a problem on the Report Version.
- Week Numbers are Duplicated.
Below is the code that controls the fields:
		Code:
	
	
	Private Sub Form_Open(Cancel As Integer)
' From Access 2000 Developer's Handbook, Volume I
' by Getz, Litwin, and Gilbert (Sybex)
' Copyright 1999.  All rights reserved.
	
' You didn't know how many columns, or what
' their names would be, until now.
' Fill in the label captions,
' and control ControlSources.
	
	Dim intColCount As Integer
	Dim intControlCount As Integer
	Dim i As Integer
	Dim strName As String
	Dim lines As Integer
	Dim MyDb As Database
	Dim rst As Recordset
	
	On Error GoTo HandleErr
' TO DO: Turn normal error handler on when this condition is finished.
	On Error Resume Next
	
	
	Dim MyQuery As QueryDef
	
'  Set database variable to current database.
	Set MyDb = DBEngine.Workspaces(0).Databases(0)
	
'  Open QueryDef.
	Set MyQuery = MyDb.QueryDefs("[qryProdValue20WeekSchedule]")
	
'  Open Recordset.
	Set rst = MyQuery.OpenRecordset()
	rst.MoveFirst
	
	
	intColCount = rst.Fields.COUNT
	intControlCount = Me.Detail.Controls.COUNT
	
        If intControlCount < intColCount Then
                 intColCount = intControlCount
        End If
	
' Fill in information for the necessary controls.
	For i = 12 To intColCount
		strName = rst.Fields(i).Name
		Me.Controls("lblHeader" & (i)).Caption = strName
		Me.Controls("txtData" & (i)).ControlSource = strName
		Me.Controls("txtSum" & (i)).ControlSource = _
		"=Sum([" & strName & "])"
		Me.Controls("txtData" & (i)).Properties("AggregateType").VALUE = 0
		Next i
		
' Hide the extra controls.
		For i = intColCount To intControlCount
			Me.Controls("txtData" & i).Visible = False
			Me.Controls("lblHeader" & i).Visible = False
			Me.Controls("txtSum" & i).Visible = False
			Next i
			
' Close the recordset.
			rst.CLOSE
			
			
			
			ExitHere:
			Exit Sub
			
' Error handling block added by Error Handler Add-In. DO NOT EDIT this block of code.
' Automatic error handler last updated at 09-09-2004 15:51:45   'ErrorHandler:$$D=09-09-2004    'ErrorHandler:$$T=15:51:45
			HandleErr:
			Select Case Err.Number
			Case Else
				MsgBox "Error " & Err.Number & ": " & Err.DESCRIPTION, vbCritical, "Report_rptProductionCapacityPlan.Report_Open"   'ErrorHandler:$$N=Report_rptProductionCapacityPlan.Report_Open
			End Select
' End Error handling block.
		End SubI have some screenshots of the Column Naming misbehavior.
From the Query:
https://ibb.co/1GKtHb1
Same Data in the Form:
https://ibb.co/CV0LpY9
Notice the duplicated Field Names & Mixed Order.
I get the concept of how this is supposed to be working but im not convinced about the column counting and field counting - if this is most effective way of achieving the goal. Im also not sure why it works fine on a report and not a form.
Any help would be greatly, greatly appreciated.
Thanks
Alex
 
	 
 
		 
 
		 
 
		 
 
		