Add Textbox at Runtime and save/retrieve to/from DB
Ok. How about this one?
I'd like to add a "Diary" feature to my app. The user would:
1) click cmdAdd, which would create a textbox at runtime
2) enter a note, comment into this new textbox
3) click cmdSave, which would add a new record to TblComments in Access 2003
-----
4) When app is reopened, create the number of textboxes needed to display these comments. (Textboxes = .Recordcount)
Anyone have an example of this?
The question is not, Can I?
The question is, How Do I?
Re: Add Textbox at Runtime and save/retrieve to/from DB
That sounds like it could result in a ridiculous number of textboxes as the diary fills up.
What you should have is a single form with a large textbox for holding comments and a label in the top right hand corner that shows the entry date for the comment being viewed. Then have arrow buttons that allow you to move back and forth through the diary like flipping pages.
See the link in my sig on ADO.NET to understand how to accomplish retrieving and saving data to your database. It should be simpler for you because you only have 2 fields that need to be saved
- VS2008 Express, Access, SQL Server 2005 Express, VB/C#/ADO.NET -
Rate posts that have been helpful to you! It's a way of giving back to someone who has helped you
Re: Add Textbox at Runtime and save/retrieve to/from DB
I think that since your main concerns are regarding the creation of textboxes in vb6, this thread would be better serviced in the vb6 forum. I suggest asking one of the moderators to move it.
- VS2008 Express, Access, SQL Server 2005 Express, VB/C#/ADO.NET -
Rate posts that have been helpful to you! It's a way of giving back to someone who has helped you
Re: Add Textbox at Runtime and save/retrieve to/from DB
There are various ways to create textboxes (and other controls) at run-time from code, in our Classic VB FAQs (link in my signature), there is a link in the Controls section to a tutorial which explains the usual methods.
However, I also am unsure if it would be the best display method for this kind of thing. One issue is that you could easily get to the stage where there are several entries, and not all of the textboxes will fit on the form - so you would need to implement some sort of scrolling (or move, etc).
I would instead use some sort of grid based control, as that will already deal with that kind of issue, and depending on the fields you are have (date?) could also provide much better navigation (perhaps expandable sections for each month or week - which would be easy with a Treeview).
The easiest method for editing may well be a textbox, but that does not need to be your display method too.
Re: Add Textbox at Runtime and save/retrieve to/from DB
If users don't have to edit previous entries, just allow their display, you could use a report in Access. Or perhaps a continuous form? Haven't used one of those in so long I'd have to go back & look.
Re: Add Textbox at Runtime and save/retrieve to/from DB
OK. The way I explained it originally is how I'm building it. This is how the end user wants it. I have explained the issues brought to my attention in this thread and he doesn't care.
What we are talking about here is occasionaly entries maybe two or three a month. So 36 or so per property and there's only 5 properties.
So far, I have code to determine the number of records in the SQL recordset. The number of records determines the number of textboxes to build in the array.
So now I'm looking to populate the array with the recordset.
The question is not, Can I?
The question is, How Do I?
Re: Add Textbox at Runtime and save/retrieve to/from DB
Assuming that you are working with a dataset, you technically don't need an array. You should enable AutoScroll for your form and then use something like this:
vb.net Code:
Dim y As Integer = 0 'Keeps track of the y coordinate of the last textbox
Dim Z As Integer
For Z = 0 To ds.Tables(0).Rows.Count - 1 'Looping through the records
Dim txt As New TextBox
With txt
.MultiLine = True
.Name = "txtbox" & Z
.Text = ds.Tables(0).Rows(Z).Item("DiaryEntry").ToString 'Fill it with information from row Z
.Size = New Size(Me.Width - 25, 200)
.Location = New Point(0, y) 'Move it so that it doesn't overlap any other textbox
End With
y = y + 205 'Increment y to reflect the latest textbox position and add a little gap
Next
Last edited by MaximilianMayrhofer; Nov 25th, 2007 at 03:52 AM.
- VS2008 Express, Access, SQL Server 2005 Express, VB/C#/ADO.NET -
Rate posts that have been helpful to you! It's a way of giving back to someone who has helped you
Re: Add Textbox at Runtime and save/retrieve to/from DB
Well, like si says, you'd also have to increase your form size at the same time. I don't know if there's a limit to this in Access 2003, except I ran into one with Access 97.
It can be difficult to work with end users who want things that just can't be done, at least at the cost they're willing to pay.
Re: Add Textbox at Runtime and save/retrieve to/from DB
No you don't have to increase your form size, you just have to enable autoscroll. This means that with a smaller form, you can still display lots and lots of records. My code sample works great for my test database with over 100 records.
- VS2008 Express, Access, SQL Server 2005 Express, VB/C#/ADO.NET -
Rate posts that have been helpful to you! It's a way of giving back to someone who has helped you
Re: Add Textbox at Runtime and save/retrieve to/from DB
aaarrrrggghhhhhh.... It's so cute and compact, too.... Can't it be modified in some way?
What about putting the textboxes on a panel? Are there panels in vb6? Can a panel have a scroller?
Or just make one textbox with scroll enabled, and separate entries with a line like:
entry1
_________________________
entry2
_________________________
Edit: Actually, why not just recode your project using VS2008 Express and then you can use .NET, the little code chunk I posted and the autoscroll feature! And you can even include the required framework as part of the installer if your client's computer doesn't have it.
Last edited by MaximilianMayrhofer; Nov 25th, 2007 at 10:36 AM.
- VS2008 Express, Access, SQL Server 2005 Express, VB/C#/ADO.NET -
Rate posts that have been helpful to you! It's a way of giving back to someone who has helped you
Re: Add Textbox at Runtime and save/retrieve to/from DB
Well, Thanks everyone but I have it working. See attached, I don't normally attach files, so I hope this works.
After I had this working, I moved it to my main app and added vertical scroll bars to the textboxes. It works exactly they way I was looking for it to.
Of course to see it work, you'll need a an .mdb with a Comments table that has the following fields (Date, Comment(Memo), and PropertyCommonName. Just set the path in the code to where you build it.
Last edited by jhize; Nov 26th, 2007 at 12:25 PM.
The question is not, Can I?
The question is, How Do I?
Re: Add Textbox at Runtime and save/retrieve to/from DB
As you haven't included the Database and Module1.bas, we can't run it - but I have read your code, and it looks good apart from some (relatively minor) issues - which may be due to this being an early 'test' version.
The biggest issue is that you don't close the recordset (or connection) when finished with it. This can causes memory issues, and as you are using an Access database also means that you could easily corrupt it. The correct way to do it (using the end of your frmAddComment.Command1_Click sub as an example) is like this:
Code:
End With
rst.close
set rst = Nothing
conn.close
set conn = Nothing
Unload Me
'no need for exit sub etc, as the sub will be left when you reach End Sub anyway
End Sub
It appears that you haven't declared all of your variables (such as Propertyname in frmAddComment.Command1_Click), which you should really do (to improve memory usage, speed, and readability). You may have declared them in the module, but it is better to declare them at the level they are actually used (in this case, inside Command1_Click itself). Once you have done that, you can turn on Option Explicit, which is a massive help in tracking down issues caused by typos.
You also haven't indented your code much (and in most places, haven't got comments), which makes it harder to read.
You used the dreaded GoTo, which makes things harder to read - and can almost always be easily avoided. In frmCommentor.Form_Load you have code like this:
Code:
'===========Checks for empty recordset
If rst.BOF = True Then
If rst.EOF = True Then
GoTo skip
End If
End If
'===========Determines number of records
...
Next x
Exit Sub '(as there is no code after the label, this line is irrelevant!)
skip:
End Sub
..which can be easily altered to this clearer version:
Code:
'===========Checks for empty recordset
If Not(rst.BOF = True And rst.EOF = True) Then 'or: If Not(rst.BOF And rst.EOF) Then
'===========Determines number of records
...
Next x
End If
End Sub
Re: Add Textbox at Runtime and save/retrieve to/from DB
Wow! Again, I appreciate your help.
I re-attached the zip file with Module1.bas and the mdb included (Commentor.mdb) - path name in code will need to be changed. I just want you to see how it works.
I will make the corrections to my code as you detailed above. I will post it here again when complete and mark as resolved. I'm not sure if anyone else would need something like this but it works the way I intended.
The question is not, Can I?
The question is, How Do I?