Creating a table similar to word in access form (1 Viewer)

Spam808

Registered User.
Local time
Today, 14:10
Joined
Dec 3, 2018
Messages
55
I understand when I say the word table, people immediately think I am talking about creating an access table or displaying data from table on table.

I want to create a table in my form, like you would do in Microsoft Word. How would you do this?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:10
Joined
Aug 30, 2003
Messages
36,123
Presuming you mean to display some data, perhaps a subform?
 

Spam808

Registered User.
Local time
Today, 14:10
Joined
Dec 3, 2018
Messages
55
I don't want to display data or a sub form. I want to create a table like you would in word with headers to enter data that is going no where. Its like a calculator or how excel operates, say for example row one has 5 pencils, 4 erasers, 3 pens and in the total it will show 12.

It is just going to be empty with headers to do calculation as you type in numbers.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:10
Joined
Aug 30, 2003
Messages
36,123
Then a grid of textboxes in whatever pattern you want. The "totals" textbox would refer to the others.
 

Spam808

Registered User.
Local time
Today, 14:10
Joined
Dec 3, 2018
Messages
55
Then a grid of textboxes in whatever pattern you want. The "totals" textbox would refer to the others.

Say I have a drop down menu to select number of rows needed with 8 textboxes. It is one of those options, it is not need on the form unless its part of the customers plan. It is hidden in the background, unless you select the number of rows needed. Is there a vba coding for such thing?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:10
Joined
Aug 30, 2003
Messages
36,123
Well, you could have many rows of textboxes and hide them. Make the appropriate number visible based on the drop down. It's odd that this is all to be unbound (data not going anywhere), but you know your needs. Or base a subform on a local table that you add x rows to based on the combo. Even though the subform would be bound to the local table, the data wouldn't go anywhere.
 

Spam808

Registered User.
Local time
Today, 14:10
Joined
Dec 3, 2018
Messages
55
Thank you pbaldy, I will pay for your support. Is it possible, if you could teach me how to write the code for such thing? I am one of those person if you give an example, I will try it out and follow your instructions.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:10
Joined
Feb 19, 2002
Messages
43,203
Spam,
This isn't the kind of project that Access excels at. It will take a lot of code and will prevent the app from being distributed as an .accde or being run using the Access Runtime engine.

You might be able to get the same effect with little or no coding by simply using a subform in DS view bound to a table.

Unlike VB, Access forms will not allow you to create an array of controls. You can name your controls with numeric suffixes but you still can't use array processing.
 

Spam808

Registered User.
Local time
Today, 14:10
Joined
Dec 3, 2018
Messages
55
If it's not going to work, can I take an excel form and make it a form access?
 
Last edited:

Minty

AWF VIP
Local time
Today, 22:10
Joined
Jul 26, 2013
Messages
10,367
Why not simply use Excel with macro's to control the display.
Access is a database, not a scratch pad for sums, and as such isn't well suited to that type of application.

Perhaps if you told us the end goal of your project in plain English, we could point you in a more suitable direction?
 

Spam808

Registered User.
Local time
Today, 14:10
Joined
Dec 3, 2018
Messages
55
I already explained my goal. As explained earlier, I am looking to create a simple table on my form.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:10
Joined
Aug 30, 2003
Messages
36,123
It seems simple to you because you have the concept in your mind. To us it sounds confusing. Why don't you lay out textboxes in the grid you're imagining plus the combo and attach the db here. I'll show you how to hide/display based on your combo.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:10
Joined
May 21, 2018
Messages
8,519
As said Access unfortunately does not have an unbound grid control. If you wanted to do this in something like VB.net it would be easy using an unbound grid control. I think you could fake it though a few different ways. What Pbaldy suggests are two possible ways.
 

Spam808

Registered User.
Local time
Today, 14:10
Joined
Dec 3, 2018
Messages
55
MajP, do you know how to do what pbaldy mention. I would like to learn how, if you could teach me?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:10
Joined
May 21, 2018
Messages
8,519
MajP, do you know how to do what pbaldy mention. I would like to learn how, if you could teach me?
It would really help if you could possibly build a fake example and show a screen shot. For most of us Access developers this is not something normally done. Without some details we would not know what would meet your needs because we will have to "fake" a table since there is not unbound grid

I kind of sort of did this once before. I created 200 command buttons (via code) and named them lblGrid1...lblGrid200. Then I stacked them and made them invisible. I let the user say how many rows and columns. It would arrange them, size them, and show them in a grid. This may be the approach you could use. You would use textboxes instead. Here is the code

Code:
Private Sub cmdMakeGrid_Click()
  Dim frm As Access.Form
  DoCmd.OpenForm "frmDefineGrid", , , , , acDialog
  If CurrentProject.AllForms("frmDefineGrid").IsLoaded Then
    Set frm = Forms("frmDefineGrid")
    If IsNumeric(frm.txtRows) And IsNumeric(frm.txtColumns) Then
       If frm.txtRows * frm.txtColumns <= 200 Then
         formatGrid frm.txtRows, frm.txtColumns
       Else
         MsgBox "Max controls is 200"
       End If
    End If
    DoCmd.Close acForm, frm.Name
  End If
End Sub

Public Sub formatGrid(x As Long, y As Long)
  Const ctlWidth = (0.75 * 1440)
  Const ctlHeight = (0.25 * 1440)
  Const conStartLeft = (0.5 * 1440)
  Const conStartTop = (1 * 1440)
  
  Dim startLeft As Long
  Dim startTop As Long
  Dim lngRow As Long
  Dim lngCol As Long
  Dim intCounter As Long
  Dim ctl As Access.Control
  
  startLeft = conStartLeft
  startTop = conStartTop
  
  For lngRow = 1 To y
     For lngCol = 1 To x
     intCounter = intCounter + 1
     Set ctl = Me.Controls("lblGrid" & intCounter)
     With ctl
         .Height = ctlHeight
         .Width = ctlWidth
         .Left = startLeft
         .Top = startTop
         .Caption = "Row" & lngRow & " Col" & lngCol
         .FontSize = 8
         .Visible = True
         .Tag = lngRow & ";" & lngCol
         'Define the function here that they do when clicked.
         .OnClick = "=gridClick()"
       End With
       startLeft = startLeft + ctlWidth
     Next lngCol
     startLeft = conStartLeft
     startTop = startTop + ctl.Height
  Next lngRow
 
 End Sub

Public Sub makeAllInvisible()
  Dim ctl As Access.Control
  'need somewhere to set focus it is hidden below the command button
  Me.txtFocus.SetFocus
  For Each ctl In Me.Controls
    If Left(ctl.Name, 7) = "lblGrid" Then
      ctl.Visible = False
    End If
  Next ctl
End Sub
Public Sub stackControls()
  Dim ctl As Access.Control
  For Each ctl In Forms("frmGrid")
    If Left(ctl.Name, 7) = "lblGrid" Then
    ctl.Left = 0
    ctl.Top = 0
    ctl.Height = 0.2 * 1440
    ctl.Width = 0.2 * 1440
    End If
  Next ctl
End Sub

Public Function gridClick()
  'This function fires when any dynamic control clicks
  Dim ctl As Access.Control
  Dim lngRow As Long
  Dim lngCol As Long
  Set ctl = ActiveControl
  
  lngRow = Split(ctl.Tag, ";")(0)
  lngCol = Split(ctl.Tag, ";")(1)

   MsgBox ctl.Name & "Row " & lngRow & " Column " & lngCol
   If ctl.ForeColor = vbRed Then
     ctl.ForeColor = vbBlack
    Else
      ctl.ForeColor = vbRed
    End If
   
End Function

Private Sub Form_Open(Cancel As Integer)
  Call makeAllInvisible
  DoEvents
  MsgBox "Click Button to create grid"
End Sub

It would do a maximum 200 controls in rows and columns.
This is pretty simple because it did not scale the controls. If you want to scale width and height this would be more involved. I can post this later and change the labels to textboxes. It will do the basics that you described. Then you would likely need code for up arrows, down arrows, tabs, etc so it acts like a table. The other potentially easier solution is using a subform, but I would have to try to demo that to see if you could get reasonable facsimile of a dynamic table.
 

Spam808

Registered User.
Local time
Today, 14:10
Joined
Dec 3, 2018
Messages
55
It seems simple to you because you have the concept in your mind. To us it sounds confusing. Why don't you lay out textboxes in the grid you're imagining plus the combo and attach the db here. I'll show you how to hide/display based on your combo.

Thank you, we are on the same page. I setup the form the phone the way you described. I just need help with explaining on how to code it. I have a combo box and 8 text boxes going across.
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:10
Joined
Feb 19, 2002
Messages
43,203
Spam,
You are talking to a bunch of Access experts. All of us agree that you can kludge something but suggest not doing this. We're not trying to make things difficult. Exactly What functionality are you trying to model? We're trying to get some explanation other than "I need a table". What is the user going to do with the table? What does your application do in general?

Your "question" is essentially - I have decided I need to use a gun to solve my problem but I need someone to help load the bullets. If you are eyeball to eyeball with a black bear, a gun might be the right solution but if you are facing a brown bear, playing dead might be the best option. Perhaps the question should be - I have this problem/feature I need to solve/create. Is there some Access feature that can help me?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:10
Joined
May 21, 2018
Messages
8,519
I am with Pat and everyone else it would help if you explained how you are using the table, because it may change a potential solution.

But on that note, I had another example that fakes this pretty well. Instead of building the grid out of multiple textboxes I have a table called tblGrid. It has one field (column) called RowNum and then fields fld1, fld2, .... to FldN where N is the number of possible columns. On the form I have textboxes txt1, txt2, ... txtN. I also have lables lbl1, lbl2,...lblN. These are all hidden. Then you specify rows and columns.
1)run delete query to delete records in tblgrid
2)do an append query to fill values in field rowNumber from 1 to number of rows
3)Bind txt1 to txtn n being number of columns to fields fld1 to fldn
4)Show and size txt1 to txtn
5)If you want you can have labels above the fields for the table header. Needs to be in a header section instead of detail. Show hide and position
6)Set allow additions and allow deletions to false

You then put this fake form as a subform on your main form. What then you would also have to code is scaling the subform control based on the size of the subform.

Now you have a fake table. This works as an input for a non-normalized presentation of the data. You can then read the table and insert into a normalized form.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:10
Joined
May 21, 2018
Messages
8,519
I have a very good working solution for what it is worth. It does what you say. takes a little formatting to make it look good. It scales the table based on number of columns. It uses the method I describe above. I will post this evening. Here is the general code
Code:
Public Sub FormatTable(numberRows As Integer, NumberColumns As Integer)
  Const TableWidth = (8 * 1440)
  Const conStartLeft = (0.5 * 1440)
  Const conFont = 12
  Const conHeight = (0.25 * 1440) 'needs to be bigger than current detail hieght or you will get a gap
  Dim ctlWidth As Double
  Dim startLeft As Long
  Dim startTop As Long
  Dim I As Integer
  Dim ctl As Access.TextBox
  Dim strSql As String
  
  makeAllInvisible
  If NumberColumns > 10 Or NumberColumns < 1 Or numberRows < 1 Then
    MsgBox "Invalid number of rows or columns. Max columns is 10. ", vbInformation, "Invalid"
    Exit Sub
  End If
  ctlWidth = TableWidth / NumberColumns
  startLeft = conStartLeft
  MakeGrid numberRows
  Me.Requery
  For I = 1 To NumberColumns
     Set ctl = Me.Controls("txt" & I)
     With ctl
         .Height = conHeight
         .Top = 0
         .Width = ctlWidth
         .Left = startLeft
         .FontSize = conFont
         .BorderColor = vbBlack
         .BorderWidth = "3"
         .Visible = True
         .Tag = ""
         .ControlSource = "fld" & I
       End With
       startLeft = startLeft + ctlWidth
  Next I
 End Sub

Private Sub makeAllInvisible()
  Dim ctl As Access.Control
  'need somewhere to set focus it is hidden below the command button
  'Stack them to get out of the way
  Me.textBox_Focus.SetFocus
  For Each ctl In Me.Controls
     If Left(ctl.Name, 3) = "txt" Then
      ctl.Visible = False
      ctl.Left = 0
      ctl.TopPadding = 0
     End If
  Next ctl
End Sub

Private Sub Form_Open(Cancel As Integer)
  Dim strSql As String
  
  Dim rs As DAO.Recordset
  Dim fld As DAO.Field
  Call makeAllInvisible
  strSql = "Delete * from tblGrid where RowNumber <> 1"
  'need 1 row or you cannot set focus to text_focus
  CurrentDb.Execute strSql
  strSql = "Select * from tblGrid"
  Set rs = CurrentDb.OpenRecordset(strSql)
  For Each fld In rs.Fields
    If fld.Name <> "RowNumber" Then
      rs.Edit
        fld = ""
      rs.Update
    End If
  Next fld
End Sub

Private Sub MakeGrid(numberRows)
  Dim strSql As String
  Dim I As Integer
  strSql = "Delete * from tblGrid"
  CurrentDb.Execute strSql
  For I = 1 To numberRows
    strSql = "Insert INTO tblGrid (RowNumber) values (" & I & ")"
   CurrentDb.Execute strSql
  Next I
 
End Sub
Public Function GetTableHeight() As Double
  GetTableHeight = Me.txt1.Height * Me.Recordset.RecordCount + 720
End Function

You call it from the main form. As so

Code:
Private Sub cmdBuild_Click()
  Dim frm As Form_frmFakeTable
  Set frm = Me.frmFakeTable.Form
  If IsNumeric(Me.txtRows) And IsNumeric(Me.txtColumns) Then
    frm.FormatTable Me.txtRows, Me.txtColumns
  End If
End Sub
 

Spam808

Registered User.
Local time
Today, 14:10
Joined
Dec 3, 2018
Messages
55
I created a table with 7 fields, and put the table in as a subform. I am receiving error too few parameters 1, run time 3061. What part of the codes do I need to rename?
 
Last edited:

Users who are viewing this thread

Top Bottom