How to use a loop through multiple similar textboxes? - asp.net-mvc

So I know this is pretty sloppy, but I have a lot going on for a simple triangulation program (2d) and I'm very new.
The user inputs value in textboxes- "aft1.text,ain1.text,aft2.text,ain2.text,bft1.text,bin1.text,bft2.text,bin1.text." This goes on for as many points that need triangulated. I want to be able to run a loop through these similar textboxes, and run a function on them.
So for each textbox that starts with abft + i and ain + i run the inches function to create a1.
Do the same to create b1.
Then another loop to plot a + i and b + i on the chart per the xcoord/ycoord functions created.
Dim a1 As Double = inches(aft1.Text, ain1.Text)
Dim b1 As Double = inches(bft1.Text, bin1.Text)
If a1 <> 0 And b1 <> 0 Then
Dim targetpoint As Int32
targetpoint = Chart1.Series("Drawing").Points.AddXY((xcoord(a1, b1)), ((ycoord(a1, b1))))
Chart1.Series("Drawing").Points.Item(targetpoint).Label = "1"
End If
Dim a2 As Double = inches(aft2.Text, ain2.Text)
Dim b2 As Double = inches(bft2.Text, bin2.Text)
If a2 <> 0 And b2 <> 0 Then
Dim targetpoint As Int32
targetpoint = Chart1.Series("Drawing").Points.AddXY((xcoord(a2, b2)), ((ycoord(a2, b2))))
Chart1.Series("Drawing").Points.Item(targetpoint).Label = "2"
End If
Dim a3 As Double = inches(aft3.Text, ain3.Text)
Dim b3 As Double = inches(bft3.Text, bin3.Text)
If a3 <> 0 And b3 <> 0 Then
Dim targetpoint As Int32
targetpoint = Chart1.Series("Drawing").Points.AddXY((xcoord(a3, b3)), ((ycoord(a3, b3))))
Chart1.Series("Drawing").Points.Item(targetpoint).Label = "3"
End If

If I understand you correctly, this code can be easily generalized by creating a Sub and calling it with your inches calculation and Label text,:oord are reachable (in scope) Functions:
Triangulate(inches(aft1.Text, ain1.Text), inches(bft1.Text, bin1.Text), 1)
Triangulate(inches(aft2.Text, ain2.Text), inches(bft2.Text, bin2.Text), 2)
Triangulate(inches(aft3.Text, ain3.Text), inches(bft3.Text, bin3.Text), 3)
EDIT-2
Although I have tested the following up to a point, since I do not have your chart properties nor the inches, xcoord and ycoord functions I could not test it completely, so try it out and let me know how it goes.
Sub TriangulateAll()
Try
Dim aft As New SortedList(Of String, TextBox)
Dim ain As New SortedList(Of String, TextBox)
Dim bft As New SortedList(Of String, TextBox)
Dim bin As New SortedList(Of String, TextBox)
For Each ctl As Control In Controls
If TypeOf (ctl) Is TextBox Then
Select Case ctl.Name.Substring(0, 3)
Case "aft"
aft.Add(ctl.Name, ctl)
Case "ain"
ain.Add(ctl.Name, ctl)
Case "bft"
bft.Add(ctl.Name, ctl)
Case "bin"
bin.Add(ctl.Name, ctl)
End Select
End If
Next
Dim a As New List(Of Double)
Dim b As New List(Of Double)
For Each kvp_aft As KeyValuePair(Of String, TextBox) In aft
For Each kvp_ain As KeyValuePair(Of String, TextBox) In ain
a.Add(inches(kvp_aft.Value.Text, kvp_ain.Value.Text))
Next
Next
For Each kvp_bft As KeyValuePair(Of String, TextBox) In bft
For Each kvp_bin As KeyValuePair(Of String, TextBox) In bin
b.Add(inches(kvp_bft.Value.Text, kvp_bin.Value.Text))
Next
Next
For i As Int16 = 0 To aft.Count - 1
Triangulate(a(i), b(i), i.ToString())
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
EDIT
Replace the 3 calls to Triangulate with the following code to completely generalize the Triangulation routine. Note that this routine expects there to be an equal number of each of the TextBox controls: aft, ain, bft, bin. I haven't tested this but it should work.
Dim aft As List(Of TextBox) = Nothing
Dim ain As List(Of TextBox) = Nothing
Dim bft As List(Of TextBox) = Nothing
Dim bin As List(Of TextBox) = Nothing
For Each ctl As Control In Controls
If TypeOf (ctl) Is TextBox Then
Select Case ctl.Name.Substring(0, 2)
Case "aft"
aft.Add(ctl)
Case "ain"
ain.Add(ctl)
Case "bft"
bft.Add(ctl)
Case "bin"
bin.Add(ctl)
End Select
End If
Next
aft.Sort()
ain.Sort()
bft.Sort()
bin.Sort()
For i As Int16 = 0 To aft.Count - 1
Dim a As Double = inches(aft.Item(i).Text, ain.Item(i).Text)
Dim b As Double = inches(bft.Item(i).Text, bin.Item(i).Text)
Triangulate(a, b, i.ToString())
Next
Sub Triangulate(a As Double, b As Double, LabelValue As String)
If a <> 0 And b <> 0 Then
Dim targetpoint As Int32
targetpoint = Chart1.Series("Drawing").Points.AddXY((xcoord(a, b)), ((ycoord(a, b))))
Chart1.Series("Drawing").Points.Item(targetpoint).Label = LabelValue
End If
End Sub

Related

Split Wav File in VB6

I need help to fix the attached VB6 code which is supposed to take an audio file and split it into 5 equal parts.
This is the way this code should work:
The first part starts from the beginning of Track.wav file.
The second part starts from where the first split part ended.
The third part starts from where the second split part ended.
The fourth part starts from where the third split part ended.
The fifth part starts from where the fourth split part ended.
Essentially each of the file parts is a continuation of the previous part of file split. After the split I have 1.wav, 2.wav, 3.wav. 4.wav and 5.wav all derived from a Track.wav file. The code attached already splits the file into five equal parts but the problem is that all the audio parts are the same as the first part instead of a continuation.
I need help to get this fixed to work as it should in VB6 (not .NET). I'd be grateful for your assistance with this.
Dim Wavlength As Integer
Private Sub Command1_Click()
On Error Resume Next
DoFirstWav
Me.SetFocus
End Sub
Private Sub DoFirstWav()
On Error Resume Next
Dim ByteData() As Byte
Dim FirstWav As Integer
FirstWav = Wavlength / 5
ByteData = ReadFile(App.Path & "\Track.wav", 1, FirstWav & "0000")
Call WriteFile(App.Path & "\Segments\1.wav", ByteData)
DoSecondWav
End Sub
Private Sub DoSecondWav()
On Error Resume Next
Dim ByteData() As Byte
Dim SecondWav As Integer
SecondWav = Wavlength / 5
ByteData = ReadFile(App.Path & "\Track.wav", 1, SecondWav & "0000")
Call WriteFile(App.Path & "\Segments\2.wav", ByteData)
DoThirdWav
End Sub
Private Sub DoThirdWav()
On Error Resume Next
Dim ByteData() As Byte
Dim ThirdWav As Integer
ThirdWav = Wavlength / 5
ByteData = ReadFile(App.Path & "\Track.wav", 1, ThirdWav & "0000")
Call WriteFile(App.Path & "\Segments\3.wav", ByteData)
DoFourthWav
End Sub
Private Sub DoFourthWav()
On Error Resume Next
Dim ByteData() As Byte
Dim FourthWav As Integer
FourthWav = Wavlength / 5
ByteData = ReadFile(App.Path & "\Track.wav", 1, FourthWav & "0000")
Call WriteFile(App.Path & "\Segments\4.wav", ByteData)
DoFifthWav
End Sub
Private Sub DoFifthWav()
On Error Resume Next
Dim ByteData() As Byte
Dim FifthWav As Integer
FifthWav = Wavlength / 5
ByteData = ReadFile(App.Path & "\Track.wav", 1, FifthWav & "0000")
Call WriteFile(App.Path & "\Segments\5.wav", ByteData)
MsgBox "Wav Split Successfully", vbInformation
End
End Sub
Private Function ReadFile(ByVal strFileName As String, Optional ByVal lngStartPos As Long = 1, Optional ByVal lngFileSize As Long = -1) As Byte()
On Error Resume Next
Dim FilNum As Integer
FilNum = FreeFile
Open strFileName For Binary As #FilNum
If lngFileSize = -1 Then
ReDim ReadFile(LOF(FilNum) - lngStartPos)
Else
ReDim ReadFile(lngFileSize - 1)
End If
Get #FilNum, lngStartPos, ReadFile
Close #FilNum
End Function
Private Function WriteFile(ByVal strFileName As String, ByteData() As Byte, Optional ByVal lngStartPos As Long = -1, Optional ByVal OverWrite As Boolean = True)
On Error Resume Next
Dim FilNum As Integer
FilNum = FreeFile
If OverWrite = True And Dir(strFileName) <> "" Then
Kill strFileName
End If
Open strFileName For Binary As #FilNum
If lngStartPos = -1 Then
Put #FilNum, LOF(FilNum) + 1, ByteData
Else
Put #FilNum, l, ByteData
End If
Close #FilNum
End Function
Private Sub Form_Load()
On Error Resume Next
Dim MyInt As Integer
Dim MyByte As Byte
Dim MyStr As String * 4
Dim MyLong As Long
Dim SampleRate, BytesPerSample, FileSize As Long
Open App.Path & "\Track.wav" For Binary Access Read Lock Read As #1
Get #1, , MyStr: Debug.Print "Riff = "; MyStr
Get #1, , MyLong: Debug.Print "File size = "; MyLong
FileSize = MyLong
Get #1, , MyStr: Debug.Print "Wave = "; MyStr
Get #1, , MyStr: Debug.Print "Format = "; MyStr
Get #1, , MyLong: Debug.Print "Any = "; MyLong
Get #1, , MyInt: Debug.Print "formatTag = "; MyInt
Get #1, , MyInt: Debug.Print "Channels = "; MyInt
Get #1, , MyLong: Debug.Print "Samples per Sec = "; MyLong
SampleRate = MyLong
Get #1, , MyInt: Debug.Print "Bytes per Sec = "; MyInt
Get #1, , MyInt: Debug.Print "BlockAlign = "; MyInt
Get #1, , MyInt: Debug.Print "Bytes per Sample = "; MyInt
BytesPerSample = MyInt
Close #1
Wavlength = FileSize / (SampleRate * BytesPerSample)
End Sub
This question is quite involved, particularly if each part needs to be playable. The reason is that each file you create needs to have a valid header record. To complicate it further, it appears a header record may be 44 bytes, 46 bytes, or even other sizes.
I worked out some basic code based on your post that appears to work for the wav file I tested:
Option Explicit
Private Const HEADER_SIZE As Long = 46
Private Const CHUNK_COUNT As Long = 5
Private HeaderData(HEADER_SIZE) As Byte
Private ChunkSize As Long
Private Sub Form_Load()
Dim MyInt As Integer
Dim MyByte As Byte
Dim MyStr As String * 4
Dim MyLong As Long
Dim FileSize As Long
Open App.Path & "\Track.wav" For Binary Access Read Lock Read As #1
Get #1, , MyStr: Debug.Print "Riff = "; MyStr
Get #1, , MyLong: Debug.Print "File size = "; MyLong
Get #1, , MyStr: Debug.Print "Wave = "; MyStr
Get #1, , MyStr: Debug.Print "Format = "; MyStr
Get #1, , MyLong: Debug.Print "Any = "; MyLong
Get #1, , MyInt: Debug.Print "formatTag = "; MyInt
Get #1, , MyInt: Debug.Print "Channels = "; MyInt
Get #1, , MyLong: Debug.Print "Samples per Sec = "; MyLong
Get #1, , MyInt: Debug.Print "Bytes per Sec = "; MyInt
Get #1, , MyInt: Debug.Print "BlockAlign = "; MyInt
Get #1, , MyInt: Debug.Print "Bytes per Sample = "; MyInt
Get #1, , MyInt: Debug.Print "Something = "; MyInt 'for my wave file, I needed 2 extra bytes
Get #1, , MyStr: Debug.Print "SubchunkID = "; MyStr
Get #1, , FileSize: Debug.Print "SubchunkSize = "; FileSize
Get #1, 1, HeaderData 'the size changes depending upon the file
Close #1
ChunkSize = CLng(FileSize / CHUNK_COUNT) 'you might loose some data here
End Sub
Private Sub Command1_Click()
Dim i As Integer
Dim ByteData() As Byte
Dim StartPos As Long
For i = 1 To CHUNK_COUNT
StartPos = HEADER_SIZE + ((i - 1) * ChunkSize)
ByteData = ReadFile(App.Path & "\Track.wav", StartPos, ChunkSize)
Call WriteFile(App.Path & "\Segments\" & i & ".wav", HeaderData, ByteData)
Next
MsgBox "Wav Split Successfully", vbInformation
End
End Sub
Private Function ReadFile(ByVal strFileName As String, ByVal lngStartPos As Long, ByVal lngFileSize As Long) As Byte()
On Error Resume Next
Dim FilNum As Integer
FilNum = FreeFile
ReDim ReadFile(lngFileSize - 1)
Open strFileName For Binary As #FilNum
Get #FilNum, lngStartPos, ReadFile
Close #FilNum
End Function
Private Function WriteFile(ByVal strFileName As String, HeaderData() As Byte, ByteData() As Byte, Optional ByVal OverWrite As Boolean = True)
On Error Resume Next
Dim FilNum As Integer
FilNum = FreeFile
If OverWrite = True And Dir(strFileName) <> "" Then
Kill strFileName
End If
Open strFileName For Binary As #FilNum
Put #FilNum, LOF(FilNum) + 1, HeaderData
Put #FilNum, HEADER_SIZE, ByteData
Close #FilNum
End Function
I eliminated a lot of duplicate code by implementing a For loop. In that loop, I calculate the Start position for the Read, and also pass the header record for the Write.
Again, I stress that this is very basic and will not work for all wav files. You can manually adjust the HEADER_SIZE if it does not work for your file.
Likely the header record needs to be modified to reflect the correct size of the new file, instead of using the header from the original file.
This should get you started.
This is the working code. I'm sure someone may need something like this in future, si thought I'd post it here.
Dim Wavlength As Long
Dim PartLength As Integer
Dim WavHeader() As Byte
Private Sub Command1_Click()
On Error Resume Next
WavHeader = ReadFile(App.Path & "\Track.wav", 1, 320)
PartLength = Wavlength / 6 - 2
DoFirstWav
End Sub
Private Sub DoFirstWav()
On Error Resume Next
Dim ByteData() As Byte
Dim FirstWav As Integer
ByteData = ReadFile(App.Path & "\Track.wav", 1, PartLength & "0000")
Call WriteFile(App.Path & "\Segments\1.wav", ByteData)
DoSecondWav
End Sub
Private Sub DoSecondWav()
On Error Resume Next
Dim ByteData() As Byte
Dim ByteRead() As Byte
Dim SecondWav As Integer
SecondWav = PartLength
ByteRead = ReadFile(App.Path & "\Track.wav", SecondWav & "0000", PartLength & "0000")
ReDim ByteData(UBound(WavHeader) + UBound(ByteRead)) As Byte
For i = 0 To UBound(WavHeader)
ByteData(i) = WavHeader(i)
Next i
For i = 0 To UBound(ByteRead)
ByteData(UBound(WavHeader) + i) = ByteRead(i)
Next i
Call WriteFile(App.Path & "\Segments\2.wav", ByteData)
DoThirdWav
End Sub
Private Sub DoThirdWav()
On Error Resume Next
Dim ByteData() As Byte
Dim ByteRead() As Byte
Dim ThirdWav As Integer
ThirdWav = PartLength * 2 + 1
ByteRead = ReadFile(App.Path & "\Track.wav", ThirdWav & "0000", PartLength & "0000")
ReDim ByteData(UBound(WavHeader) + UBound(ByteRead)) As Byte
For i = 0 To UBound(WavHeader)
ByteData(i) = WavHeader(i)
Next i
For i = 0 To UBound(ByteRead)
ByteData(UBound(WavHeader) + i) = ByteRead(i)
Next i
Call WriteFile(App.Path & "\Segments\3.wav", ByteData)
DoFourthWav
End Sub
Private Sub DoFourthWav()
On Error Resume Next
Dim ByteData() As Byte
Dim ByteRead() As Byte
Dim FourthWav As Integer
FourthWav = PartLength * 3 + 1
ByteRead = ReadFile(App.Path & "\Track.wav", FourthWav & "0000", PartLength & "0000")
ReDim ByteData(UBound(WavHeader) + UBound(ByteRead)) As Byte
For i = 0 To UBound(WavHeader)
ByteData(i) = WavHeader(i)
Next i
For i = 0 To UBound(ByteRead)
ByteData(UBound(WavHeader) + i) = ByteRead(i)
Next i
Call WriteFile(App.Path & "\Segments\4.wav", ByteData)
DoFifthWav
End Sub
Private Sub DoFifthWav()
On Error Resume Next
Dim ByteData() As Byte
Dim ByteRead() As Byte
Dim FifthWav As Integer
FifthWav = PartLength * 4 + 1
ByteRead = ReadFile(App.Path & "\Track.wav", FifthWav & "0000", PartLength & "0000")
ReDim ByteData(UBound(WavHeader) + UBound(ByteRead)) As Byte
For i = 0 To UBound(WavHeader)
ByteData(i) = WavHeader(i)
Next i
For i = 0 To UBound(ByteRead)
ByteData(UBound(WavHeader) + i) = ByteRead(i)
Next i
Call WriteFile(App.Path & "\Segments\5.wav", ByteData)
End Sub
Private Function ReadFile(ByVal strFileName As String, Optional ByVal lngStartPos As Long = 1, Optional ByVal lngFileSize As Long = -1) As Byte()
On Error Resume Next
Dim FilNum As Integer
FilNum = FreeFile
Open strFileName For Binary As #FilNum
If lngFileSize = -1 Then
ReDim ReadFile(LOF(FilNum) - lngStartPos)
Else
ReDim ReadFile(lngFileSize - 1)
End If
Get #FilNum, lngStartPos, ReadFile
Close #FilNum
End Function
Private Function WriteFile(ByVal strFileName As String, ByteData() As Byte, Optional ByVal lngStartPos As Long = -1, Optional ByVal OverWrite As Boolean = True)
On Error Resume Next
Dim FilNum As Integer
FilNum = FreeFile
If OverWrite = True And Dir(strFileName) <> "" Then
Kill strFileName
End If
Open strFileName For Binary As #FilNum
If lngStartPos = -1 Then
Put #FilNum, LOF(FilNum) + 1, ByteData
Else
Put #FilNum, l, ByteData
End If
Close #FilNum
End Function
Private Sub Form_Load()
On Error Resume Next
Dim MyInt As Integer
Dim MyByte As Byte
Dim MyStr As String * 4
Dim MyLong As Long
Dim SampleRate, BytesPerSample, FileSize As Long
Open App.Path & "\Track.wav" For Binary Access Read Lock Read As #1
Get #1, , MyStr: Debug.Print "Riff = "; MyStr
Get #1, , MyLong: Debug.Print "File size = "; MyLong
FileSize = MyLong
Get #1, , MyStr: Debug.Print "Wave = "; MyStr
Get #1, , MyStr: Debug.Print "Format = "; MyStr
Get #1, , MyLong: Debug.Print "Any = "; MyLong
Get #1, , MyInt: Debug.Print "formatTag = "; MyInt
Get #1, , MyInt: Debug.Print "Channels = "; MyInt
Get #1, , MyLong: Debug.Print "Samples per Sec = "; MyLong
SampleRate = MyLong
Get #1, , MyInt: Debug.Print "Bytes per Sec = "; MyInt
Get #1, , MyInt: Debug.Print "BlockAlign = "; MyInt
Get #1, , MyInt: Debug.Print "Bytes per Sample = "; MyInt
BytesPerSample = MyInt
Close #1
Wavlength = FileSize \ (SampleRate * BytesPerSample)
Debug.Print "Wavlength"; Wavlength
End Sub

VBA parse string Place values to new columns

I have a sheets with the rows of data like.
NOM(LSL,USL)=207.3980(206.1990,208.5970) NOM(LSL,USL)=207.3980(206.1990,208.5970) NOM(LSL,USL)=18.8200(18.4400,19.2100)
I would like to just grab the Values and place them in their own cells like
207.3980 207.3980 18.8200
206.1990 206.1990 18.4400
208.5970 208.5970 19.2100
I continue to recieve "ByRef Argument Mismatch" errors. I believe relating to how I am defining the reference cell.
Sub Parse_Replace()
Dim i As Double
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
Dim Col As Range
Dim rLastCell As Range
Set rLastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
For i = rLastCell.Column To 1 Step -1
Col = ColLett(rLastCell.Column)
Columns(i).Cells(4) = SplitString(Col3, ",", 4)
Columns(i).Cells(5) = SplitString(Col3, ",", 5)
Columns(i).Cells(6) = SplitString(Col3, ",", 6)
Next i
End Sub
Function ColLett(Col As Integer) As String
If Col > 26 Then
ColLett = ColLett((Col - (Col Mod 26)) / 26) + Chr(Col Mod 26 + 64)
Else
ColLett = Chr(Col + 64)
End If
End Function
Function SplitString(pValue As String, pChar As String, pIndex As Integer) As Variant
Dim YString As Variant
YString = Replace(Replace(Replace(Replace(pValue, " ", ""), "=", ""), "(", ","), ")", ",")
SplitString = Split(YString, pChar)(pIndex - 1)
End Function
Process
Establish number of Columns with Data
Loop through each column
Convert column index to Column with ColLett
Set cell value with SplitString
Loop
Thank You
EDIT : replaced SplitString value with inteded.
You declare Col to be a range here:
Dim Col As Range
You then try to set Col to a string here:
Col = ColLett(rLastCell.Column)
When you set a range you have to set it to a range. Furthermore, you have to use the SET keyword to do so:
Set Col = <a range>
When you set that Col you set it only to the rLastCell.Column repeatedly in each loop of your For. If you just need that column letter for the last column, then do it before entering your for loop.
All of that is pointless anyway. At no point do you use the Column letter that you went through the trouble retrieving in your function. And really, for what you are doing you don't need the Column Letter. Column Letters are for humans; the column number is what is important in VBA any how.

Masking textbox to accept only 1 digit after decimal

I am using VB6 and I want to allow user to enter only one digit after decimal,
please help.
Put the code below in change event of textbox. I think "." point is used for decimal seperator. if "," coma is used, then change the point in the code with coma.
Private Sub TextBox1_Change()
Dim strA As String
Dim intP As Integer
strA = TextBox1.Text
intP = InStr(1, strA, ".", vbTextCompare)
If intP > 0 Then TextBox1.Text = Left(strA, intP + 1)
End Sub

How do I add the total the value of a range of cells, when they contain both numbers and letters?

for example my cell contains T 6.5 I want to look for all cells in a row that contain T and add the values of the numbers also contained in that cell.
If you don't mind a row beneath your data to help total things, I would suggest using the following as a quick solution.
Assuming that your data is in Row 1 (starting in cell A1), insert the below formula in Row 2 (cell A2) and copy it to the right as far as you have data in Row 1.
=IF(IFERROR(SEARCH("T ",A1),0)<>0,VALUE(SUBSTITUTE(A1,"T ","")),0)
From there you can total the values across Row 2 using this:
=SUM(2:2)
Note that I assumed that there was a space after "T" in your example above and that this is explicitly included in the first formula above. It simply strips that text from the cell and adds up the remaining numerical value IF the cells in Row 1 have a "T " in them.
Hope this helps or points you in the right direction.
Cheers!
Here is an example for row #7
Sub SumARow()
Dim roww As Long, r As Range, _
Zum As Double, v As Variant
roww = 7
For Each r In Cells(roww, 1).EntireRow.Cells
v = CStr(r.Value)
If InStr(1, v, "T") > 0 Then
Zum = Zum + GetNumber(v)
End If
Next r
MsgBox Zum
End Sub
Public Function GetNumber(s As Variant) As Double
Dim msg As String, i As Long
GetNumber = 0
msg = ""
For i = 1 To Len(s)
ch = Mid(s, i, 1)
If ch Like "[0-9]" Or ch = "." Then
msg = msg & ch
End If
Next i
If msg = "" Then Exit Function
GetNumber = CDbl(msg)
End Function

How to resize side by side controls in MS ACCESS 2007

I have a form in which there are various fields, for ex. Two textboxes are side by side.
These 2 textbox have anchor property left,top and other right,top.
Now when I resize the form the controls are aligned to left and the other textbox to right.
But when as screen is maximized it leaves a blank space in between these two textboxes.
So then I made the anchor property of both textbox to both,both the controls overlapped.
PS: working on MS ACCESS 2007.
anchoring property above is Horizontal, Vertical
EDIT : In Normal window
_______________________Min Max Close_
| First_Name TEXTBOX Last_Name TEXTBOX |
|_______________________________|
When Maximized to whole screen it gives me
_____________________________________Min Max Close_
| First_Name TEXTBOX ............................. Last_Name TEXTBOX |
|______________________________________________|
And I need this way as below
_____________________________________Min Max Close_
| F i r s t_N a m e T E X T B O X ........ L a s t_N a m e T E X T B O X |
|______________________________________________|
I am trying to explain by doing all this as I am not allowed to upload a image, Sorry for that....
Paste the following code into your form, change the field names, and see what happens. The two fields will 'grow' as you increase the form width, yet maintain their Anchor. Note: I updated on 3/3 to handle the field labels.
Option Compare Database
Option Explicit
Dim fviInsideWidth As Integer
Dim fviSaveInsideWidth As Integer
Dim fviFormWidth As Integer
Dim fviFldWidth As Integer
Dim fviFieldGap As Integer
Dim fviRemainder As Integer
Dim fviLblWidth As Integer
Dim fviRLblToTxt As Integer
Dim fvstrLLabel As String
Dim fvstrRLabel As String
Private Sub Form_Open(Cancel As Integer)
fviSaveInsideWidth = Me.InsideWidth
fviInsideWidth = Me.InsideWidth
fviFormWidth = Me.Width
fviFldWidth = Me.fldLeft.Width + Me.fldRight.Width
fviRemainder = fviInsideWidth - fviFldWidth
fviFieldGap = Me.fldRight.Left - (Me.fldLeft.Left + Me.fldLeft.Width)
fvstrLLabel = Me.fldLeft.Controls.Item(0).Name
fvstrRLabel = Me.fldRight.Controls.Item(0).Name
fviLblWidth = Me.Controls(fvstrRLabel).Width
fviRLblToTxt = Me.fldRight.Left - Me.Controls(fvstrRLabel).Left
'Debug.Print "Open - InsideWidth = " & fviInsideWidth & " Fields: " & fviFldWidth & " Remainder: " & fviRemainder
'Debug.Print "Open - Form Width = " & Me.Width & vbTab & "Diff = " & fviInsideWidth - fviFormWidth
End Sub
Private Sub Form_Close()
Me.fldLeft.Width = fviFldWidth
Me.fldRight.Width = fviFldWidth
Me.InsideWidth = fviSaveInsideWidth
End Sub
Private Sub Form_Resize()
Dim ifldWidth As Integer
Dim ifrmWidth As Integer
fviInsideWidth = Me.InsideWidth
ifrmWidth = fviInsideWidth - 1110
Me.Width = ifrmWidth
ifldWidth = Int((fviInsideWidth - fviRemainder) / 2)
Me.fldLeft.Width = ifldWidth
Me.fldRight.Left = Me.fldLeft.Left + Me.fldLeft.Width + fviFieldGap
Me.Controls(fvstrRLabel).Left = Me.fldRight.Left - fviRLblToTxt
Me.fldRight.Width = ifldWidth
'Debug.Print "Resize - InsideWidth = " & fviInsideWidth & vbTab & "Form Width = " & Me.Width & " Flds: " & ifldWidth & " Right=" & Me.fldLeft.Left + Me.fldLeft.Width + fviFieldGap
'Debug.Print "Resize Form: " & Me.Width & " Flds: " & ifldWidth
Me.Repaint
End Sub

Resources