I have a database & all information is added to the database via forms. The code uses offset to transfer entered information from textboxes into the relevent columns on the database.
My code works fine. However, several times a year the structure of the database alters (columns are added or removed). When alterations occur I have to go into vba and alter all the offsets.
This can be very time consuming. I've tried to look at ways to adjust the code so that it offsets to a named range by defining a column (eg. Column A defined as DBProductName) but I can't seem to get my head around how to correctly code it.
Below is a sample of the code I'm using that works if the columns are static. What I need is to adjust it to work with a more dynamic database. Any help would be fantastic.
Thanks in advance
Baziwan
My code works fine. However, several times a year the structure of the database alters (columns are added or removed). When alterations occur I have to go into vba and alter all the offsets.
This can be very time consuming. I've tried to look at ways to adjust the code so that it offsets to a named range by defining a column (eg. Column A defined as DBProductName) but I can't seem to get my head around how to correctly code it.
Below is a sample of the code I'm using that works if the columns are static. What I need is to adjust it to work with a more dynamic database. Any help would be fantastic.
Code:
Private Sub cmdAdd_Click()
Dim ws As Worksheet
Application.ScreenUpdating = False
Set ws = Worksheets("Database")
With Me.txtProductName
With ws.Range(Me.cbxVineyard & (Me.cbxCategory) & ("Sort")).Rows(1)
On Error Resume Next
.Offset(1, 0).EntireRow.Insert shift:=xlUp
.Columns(1).Offset(1, 0).Value = Me.txtProductName.Value 'column A
.Columns(1).Offset(1, 1).Value = Me.txtProductCode.Value 'column B
.Columns(1).Offset(1, 4).Value = Me.cbxCountry.Value 'column E
.Columns(1).Offset(1, 5).Value = Me.cbxVineyard.Value 'column F
.Columns(1).Offset(1, 6).Value = Me.cbxVariety.Value 'column G
End With
ws.Range(Me.cbxVineyard & (Me.cbxCategory) & ("Sort")).Select
Selection.Sort Key1:=Range(Me.cbxVineyard & (Me.cbxCategory) & ("Sort")), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End With
Application.ScreenUpdating = True
Unload Me
End Sub
Thanks in advance
Baziwan